# How pass the params :id to next action? \[again\]

**URL:** <https://www.ruby-forum.com/t/how-pass-the-params-id-to-next-action-again/196566>\
**Category:** Rails\
**Created:** [January 11, 2008, 1:05am UTC](https://www.ruby-forum.com/t/how-pass-the-params-id-to-next-action-again/196566 "2008-01-11T01:05:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 11, 2008, 1:05am UTC](https://www.ruby-forum.com/t/how-pass-the-params-id-to-next-action-again/196566/1 "2008-01-11T01:05:33Z")

</div>

Hi, everybody:  
I made a view - works as creating a new comment for a specific  
article, a example URL is “[http://localhost:3000/comments/new/2](http://localhost:3000/comments/new/2)”:  
here, comments is the controller, new is the action, 2 is the id of  
the specific article, which needs to be passed to next action  
“create”, so that this comment will be attached and displayed under  
2nd article. However, I didnt figure out how I can pass the id to next  
action named “create” in this controller, seems like only the  
“comments” is the only parameter when I click the “create” button…  
what can I do?

# the new view as follow:

# New comment

\<%= error\_messages\_for :comment %\>

\<% form\_for(@comment) do |f| %\>

**Desc**  
 \<%= f.text\_area :desc %\>

\<%= f.submit "Create" %\>

\<% end %\> ==================================

Thanks dudes!  
Myst

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 11, 2008, 1:09am UTC](https://www.ruby-forum.com/t/how-pass-the-params-id-to-next-action-again/196566/2 "2008-01-11T01:09:39Z")

</div>

To Ryan:  
Suddenly, I realise that your previous answer was not really what I  
asked…  
Here, the id “2” in “[http://localhost:3000/comments/new/2](http://localhost:3000/comments/new/2)” is already  
what I would like to send to next action, but if i do " s =  
params[:id]" in next action, it says id is not the parameter  
delivered , only the params[:comment] is. how could I let the next  
action get this “2” value??

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 11, 2008, 6:17pm UTC](https://www.ruby-forum.com/t/how-pass-the-params-id-to-next-action-again/196566/3 "2008-01-11T18:17:05Z")

</div>

Simplest answer is to add something like this to your form:

\<%= f.hidden\_field :article\_id, params[:id] %\>

which would give your create action params[:comment][:article\_id]

More complicated answer is to rework your routes and controller code  
to handle nested resources for comments. i.e. /articles/2/comments/new  
instead of /comments/new/2

-Justin B.

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 11, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/how-pass-the-params-id-to-next-action-again/196566/4 "2008-01-11T18:27:41Z")

</div>

# btw, if I try your simple answer, add the “hidden\_field” in comment form. However, it errors as follow:

undefined method `merge’ for “2”:String

Extracted source (around line #21):

# 18: \<%= f.text\_field :img\_url %\> 19: 20: 21:

\<%= f.hidden\_field :article\_id, params[:id] %\>

  
22:  
23: 

24: \<%= f.submit “Create” %\>

Plus, I need to store the comment into database in create action,  
which actually does not has the column “article\_id”, am I able to kick  
it out before store into database? Got the feeling that this messes  
my code a bit…

---

<div class="post-metadata">

**Author:** ![myst\_tt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@myst\_tt](https://www.ruby-forum.com/u/myst_tt)\
**Post date:** [January 11, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/how-pass-the-params-id-to-next-action-again/196566/5 "2008-01-11T18:19:28Z")

</div>

Thank you Justin, actually now I am thinking of why this :id param  
is not passed to “create” action since the URL really matches the  
routing rule “controller/action/id” in my routing map, it does have  
the id. Is it because the new view contains a form?  
Ok, let’s say if I try the nested RESTful route and the URL  
changes to “tasks/1/comment/2” which matches the REST routing map  
“task/task\_id/comment/comment\_id”, then in the new view to create  
action, all these “task\_id” and “comment\_id” will be delivered to  
create action?  
I dont know if I express myself clearly, but really dont figure out  
the reason.
