I know this is a silly question, but I am just wondering when making the link between children and the parent table, for example a blog reply, is there a "best practice" way of doing this. The only way that I can think of is to create a hidden input field within <form> ex, <input type="hidden" id="post_id" value="1" /> (of course do it the ruby way). Is this the only way? Thanks for the help <% form_for :url => {:action => "create_reply"} %> <%= form.hidden_field :post_id %> <%= form.text_field :title, :size => 30 %> <%= form.text_area :content, :cols => 40, :rows => 5 %.\> <%= submit_tag "Post", :class => "submit" %> <% end %> -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Normally, one would use: <% form_for :url => {:action => "create_reply", :id => @posts} %> This generates something like: <form method="post" action="/controller/create_reply/3"> So, the actual post id is buried in the url to which you will issue your POST request. --steve Keith Lancaster-2 wrote:> > > I know this is a silly question, but I am just wondering when making the > link between children and the parent table, for example a blog reply, is > there a "best practice" way of doing this. > > The only way that I can think of is to create a hidden input field > within <form> > ex, <input type="hidden" id="post_id" value="1" /> (of course do it the > ruby way). > > Is this the only way? > > Thanks for the help > > <% form_for :url => {:action => "create_reply"} %> > <%= form.hidden_field :post_id %> > <%= form.text_field :title, :size => 30 %> > <%= form.text_area :content, :cols => 40, :rows => 5 %.\> > <%= submit_tag "Post", :class => "submit" %> > <% end %> > > -- > Posted via http://www.ruby-forum.com/. > > > > >-- View this message in context: http://www.nabble.com/-Rails--Parent-child-relations-tf3140275.html#a8703916 Sent from the RubyOnRails Users mailing list archive at Nabble.com. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks for the reply. I thought that doing it that way embeds the :id value into the params hash table through the id key ex, params[:id] Do you then have make the following setting manually? params[:post_id] = params[:id] Steve Ross wrote:> Normally, one would use: > > <% form_for :url => {:action => "create_reply", :id => @posts} %> > > This generates something like: > > <form method="post" action="/controller/create_reply/3"> > > So, the actual post id is buried in the url to which you will issue your > POST request. >-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
two ways to do what u want to do: a) hidden field. you suggested this above. b) via the controller when passing the id in :id => x ; but instead of params[:post_id] = params[:id] do @reply.post_id = params[:id].to_i # the to_i isn''t really that neccessary -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That make sense. I guess I thought rails would have it''s own way of doing this. The only other thing that I am wondering about is whether the params[:id] value must be cleared out to prevent the save method from thinking that the attempted insert is actually an update. I will have to check this out after work. Thanks for all the help. <% form_for :reply, :url => {:action => ''create_reply''} do |form| %> ... <% end %> //// def create_reply params[:post_id] = params[:id] params[:id] = nil post = Post.new(params[:reply]) post.save end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---