Passing parameters from pages

Hi guys,
just a simple question that seems unanswered on RoR books, I just
skimmed 4 of them. I’ve a Contact model and I’ve a Note model. Now I
want a link on the page that shows the contact enabling the user to add
a new note for that contact.
Now on the contact page I want to place an ‘add note’. I want the user
to write the note on a separate page. How can I pass the ID of the
contact to the page? Where should I place this ID in the form for the
note? (a hidden field?, is there an helper for this?) How should I write
the action to store this note, I mean how can I distinguish the note ID
from the contact ID?
At the end I’ve a great doubt, am I completely wrong and should I change
the way to develop in RoR? This last question is even more important so
if the right way to do what I’m asking is different please tell me.

Thank you.

So you have a link on the Contact page pointing to an “Add Note”. That
would be something like

<% link_to “Add Note”, {:controller => “notes”, :action => “create”, :
contact_id => 10} %>

This would move the relevant contact_id to the notes controller and
you can get it using params[:contact_id] in the create method like so
@note = Note.new
@note[‘contact_id’] = params[:contact_id]

Since you would have a contact_id field defined in the Note model, it
would then make sense to define a hidden field in your notes-

create.rhtml view like so
<% hidden_field :note, :contact_id %>

Should work. Let me know.

Regards,
Rajesh

Hi Rajesh,
first of all thank you!
I changed a couple of things I would like to share with the others in
the forum:

In the code:
<% link_to “Add Note”, {:controller => “notes”, :action => “create”,
:contact_id => 10} %>

I placed :contact_id = @contact.id, to make it general.

I’ve a single controller for edit and create so I modified the
controller with:

@note = Note.find_by_id(params[:id]) || Note.new
@note.contact_id = params[:contact_id] || @note.contact_id

this way it works both when creating a new note and when editing.

Nik

This would move the relevant contact_id to the notes controller and
you can get it using params[:contact_id] in the create method like so
@note = Note.new
@note[‘contact_id’] = params[:contact_id]

Since you would have a contact_id field defined in the Note model, it
would then make sense to define a hidden field in your notes-

create.rhtml view like so
<% hidden_field :note, :contact_id %>

Should work. Let me know.

Regards,
Rajesh

If you choose to use RESTful routes, you could also nest the Notes
resources within the Contact resources. In that way, the url will
contain the id for the contacts and it will be available by
implication in the notes_path that you’d use for your forms.

AndyV

K. Rajesh wrote:

So you have a link on the Contact page pointing to an “Add Note”. That

Regards,
Rajesh

That helps me a lot!

Thank you Rajesh

Perry

Sorry to hijack this, but I have a similar question that revolves
around a RESTful design:

I am only working with one class - Person. But each person can have
one parent. parent_id is used to link them to another person (each
person only has one parent).

This RESTful style link

<%= link_to “Create New Person”, new_page_path %>

will take me to the form to create a new person. But that person will
not have a parent_id set (similar to Nik’s origninal problem)

I can pass the value to the params hash using this:

<%= link_to “Create New Person”, new_page_path(:parent_id =>
@person.id) %>

But now I run into the problem that in my RESTful controller, there is
a new action for the form which then passes to the create action (to
actually create the object). How would I keep the reference to
params[:person_id] so that it could be passed on to the create action?

I like the idea that Nik has of using one action for edit and new. Is
it a necessity of RESTful design to have the standard 7 actions
(index, show, new, create, edit, update, destroy) separate or would it
be considered okay (DRY even) to combine them (or omit them in the
case of new, where a postback could be used instead of passing from
the new action to the create action)?

cheers,

DAZ

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs