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: =============================== <h1>New comment</h1> <%= error_messages_for :comment %> <% form_for(@comment) do |f| %> <p> <b>Desc</b><br /> <%= f.text_area :desc %> </p> <p> <%= f.submit "Create" %> </p> <% end %> ================================= Thanks dudes! Myst --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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" 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?? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Blake On Jan 10, 7:05 pm, myst_tt <tdxia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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: > ===============================> > <h1>New comment</h1> > > <%= error_messages_for :comment %> > > <% form_for(@comment) do |f| %> > <p> > <b>Desc</b><br /> > <%= f.text_area :desc %> > </p> > > <p> > <%= f.submit "Create" %> > </p> > <% end %> > =================================> > Thanks dudes! > Myst--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On 1月11日, 下午6时32分, Justin Blake <jbl...@gmail.com> wrote:> 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 Blake > > On Jan 10, 7:05 pm, myst_tt <tdxia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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: > > ===============================> > > <h1>New comment</h1> > > > <%= error_messages_for :comment %> > > > <% form_for(@comment) do |f| %> > > <p> > > <b>Desc</b><br /> > > <%= f.text_area :desc %> > > </p> > > > <p> > > <%= f.submit "Create" %> > > </p> > > <% end %> > > =================================> > > Thanks dudes! > > Myst--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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: </p> 20: 21: <p><%= f.hidden_field :article_id, params[:id] %></p> 22: 23: <p> 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... On 1月11日, 下午6时32分, Justin Blake <jbl...@gmail.com> wrote:> 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 Blake > > On Jan 10, 7:05 pm, myst_tt <tdxia...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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: > > ===============================> > > <h1>New comment</h1> > > > <%= error_messages_for :comment %> > > > <% form_for(@comment) do |f| %> > > <p> > > <b>Desc</b><br /> > > <%= f.text_area :desc %> > > </p> > > > <p> > > <%= f.submit "Create" %> > > </p> > > <% end %> > > =================================> > > Thanks dudes! > > Myst--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---