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

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”:
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| %>

<%= f.text_area :desc %>

<%= f.submit "Create" %>

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

Thanks dudes!

To Ryan:
Suddenly, I realise that your previous answer was not really what I
Here, the id “2” in “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??

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.

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 %>

<%= f.hidden_field :article_id, params[:id] %>


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…

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.