# Passing parameters from pages

**URL:** <https://www.ruby-forum.com/t/passing-parameters-from-pages/112328>\
**Category:** Rails\
**Created:** [August 17, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/passing-parameters-from-pages/112328 "2007-08-17T15:45:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![niky81](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/51bf81/32.png) [@niky81](https://www.ruby-forum.com/u/niky81)\
**Post date:** [August 17, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/passing-parameters-from-pages/112328/1 "2007-08-17T15:45:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![niky81](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/51bf81/32.png) [@niky81](https://www.ruby-forum.com/u/niky81)\
**Post date:** [August 18, 2007, 9:28am UTC](https://www.ruby-forum.com/t/passing-parameters-from-pages/112328/2 "2007-08-18T09:28:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![niky81](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/51bf81/32.png) [@niky81](https://www.ruby-forum.com/u/niky81)\
**Post date:** [August 20, 2007, 6:15pm UTC](https://www.ruby-forum.com/t/passing-parameters-from-pages/112328/3 "2007-08-20T18:15:17Z")

</div>

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

1. 

In the code:  
\<% link\_to “Add Note”, {:controller =\> “notes”, :action =\> “create”,  
:contact\_id =\> 10} %\>

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

1. 

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

---

<div class="post-metadata">

**Author:** ![niky81](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/51bf81/32.png) [@niky81](https://www.ruby-forum.com/u/niky81)\
**Post date:** [August 20, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/passing-parameters-from-pages/112328/4 "2007-08-20T19:39:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![niky81](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/51bf81/32.png) [@niky81](https://www.ruby-forum.com/u/niky81)\
**Post date:** [August 18, 2007, 3:14pm UTC](https://www.ruby-forum.com/t/passing-parameters-from-pages/112328/5 "2007-08-18T15:14:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![niky81](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/51bf81/32.png) [@niky81](https://www.ruby-forum.com/u/niky81)\
**Post date:** [August 20, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/passing-parameters-from-pages/112328/6 "2007-08-20T20:32:03Z")

</div>

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
