Hi, I have a question regarding the best way to do the following: Given I have a Parent model that has many Child models, and the "show" page for the parent model allows the user to create a new Child, which is preferable? A) <% form_for(@child, :parent_id => @parent.id) do |f| %> f.text_field :foo f.text_field :bar <% end %> -- OR -- B) <% form_for(@child) do |f| %> f.hidden_field :parent_id, :value => @parent.id f.text_field :foo f.text_field :bar <% end %> AFAIK both ways accomplish the same thing, I''m just wondering if there was a preferred "Rails" way. Thanks, Bob -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If I understand you question, the first. The second puts unnecerssary hidden field on the form and the form helper already has the I''d embeded. Only reason you would want the hidden field is if you needed it perhaps for ajax/js purposes. On 7/8/10, Bob Nadler <bnadler-Wuw85uim5zDR7s880joybQ@public.gmane.org> wrote:> Hi, > > I have a question regarding the best way to do the following: > > Given I have a Parent model that has many Child models, and the "show" > page for the parent model allows the user to create a new Child, which > is preferable? > > A) <% form_for(@child, :parent_id => @parent.id) do |f| %> > f.text_field :foo > f.text_field :bar > <% end %> > > -- OR -- > > B) <% form_for(@child) do |f| %> > f.hidden_field :parent_id, :value => @parent.id > f.text_field :foo > f.text_field :bar > <% end %> > > AFAIK both ways accomplish the same thing, I''m just wondering if there > was a preferred "Rails" way. > > Thanks, > Bob > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Sent from my mobile device -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 9 July 2010 03:42, DK <dk.kahn-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If I understand you question, the first. The second puts unnecerssary > hidden field on the form and the form helper already has the I''d > embeded. Only reason you would want the hidden field is if you needed > it perhaps for ajax/js purposes.DK: Why will @parent.id will be embedded automatically? It is not @child.parent.id that is required. Colin> > On 7/8/10, Bob Nadler <bnadler-Wuw85uim5zDR7s880joybQ@public.gmane.org> wrote: >> Hi, >> >> I have a question regarding the best way to do the following: >> >> Given I have a Parent model that has many Child models, and the "show" >> page for the parent model allows the user to create a new Child, which >> is preferable? >> >> A) <% form_for(@child, :parent_id => @parent.id) do |f| %> >> f.text_field :foo >> f.text_field :bar >> <% end %> >> >> -- OR -- >> >> B) <% form_for(@child) do |f| %> >> f.hidden_field :parent_id, :value => @parent.id >> f.text_field :foo >> f.text_field :bar >> <% end %> >> >> AFAIK both ways accomplish the same thing, I''m just wondering if there >> was a preferred "Rails" way. >> >> Thanks, >> Bob >> >> -- >> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > -- > Sent from my mobile device > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.