John Grimes
2010-Apr-12 02:13 UTC
Nested forms: Can you access the object from within fields_for?
Hi all, Say I had the following form: <% form_for @user do |f| %> <%= f.error_messages %> <% f.fields_for :friends do |friends| %> <%= friends.text_field :relationship_to_you %> <% end %> <p><%= f.submit "Submit" %></p> <% end %> Is it possible to access the current object within that fields_for block as it iterates through? For example, could I add another line that looked something like this with the friend''s name: <% form_for @user do |f| %> <%= f.error_messages %> <% f.fields_for :friends do |friends| %> <label><%= friend.name %></label> <%= friends.text_field :relationship_to_you %> <% end %> <p><%= f.submit "Submit" %></p> <% end %> Thanks for your help. John Grimes -- 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.
toamitkumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2010-Apr-12 12:31 UTC
Re: Nested forms: Can you access the object from within fields_for?
Yes u can with friend.object.name On Apr 11, 10:13 pm, John Grimes <m...-vOAwBkqxSF3QKcMg3i/TZg@public.gmane.org> wrote:> Hi all, > > Say I had the following form: > > <% form_for @user do |f| %> > <%= f.error_messages %> > <% f.fields_for :friends do |friends| %> > <%= friends.text_field :relationship_to_you %> > <% end %> > <p><%= f.submit "Submit" %></p> > <% end %> > > Is it possible to access the current object within that fields_for > block as it iterates through? > > For example, could I add another line that looked something like this > with the friend''s name: > > <% form_for @user do |f| %> > <%= f.error_messages %> > <% f.fields_for :friends do |friends| %> > <label><%= friend.name %></label> > <%= friends.text_field :relationship_to_you %> > <% end %> > <p><%= f.submit "Submit" %></p> > <% end %> > > Thanks for your help. > > John Grimes-- 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.
John Grimes
2010-Apr-13 03:49 UTC
Re: Re: Nested forms: Can you access the object from within fields_for?
That works perfectly, thanks! Just to clarify for anyone else reading this, code such as this should work: <% form_for @user do |f| %> <%= f.error_messages %> <% f.fields_for :friends do |friends| %> <label><%= friends.object.name <http://friend.name> %></label> <%= friends.text_field :relationship_to_you %> <% end %> <p><%= f.submit "Submit" %></p> <% end %> On 12 April 2010 22:31, toamitkumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <toamitkumar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Yes u can with > friend.object.name > > On Apr 11, 10:13 pm, John Grimes <m...-vOAwBkqxSF3QKcMg3i/TZg@public.gmane.org> wrote: > > Hi all, > > > > Say I had the following form: > > > > <% form_for @user do |f| %> > > <%= f.error_messages %> > > <% f.fields_for :friends do |friends| %> > > <%= friends.text_field :relationship_to_you %> > > <% end %> > > <p><%= f.submit "Submit" %></p> > > <% end %> > > > > Is it possible to access the current object within that fields_for > > block as it iterates through? > > > > For example, could I add another line that looked something like this > > with the friend''s name: > > > > <% form_for @user do |f| %> > > <%= f.error_messages %> > > <% f.fields_for :friends do |friends| %> > > <label><%= friend.name %></label> > > <%= friends.text_field :relationship_to_you %> > > <% end %> > > <p><%= f.submit "Submit" %></p> > > <% end %> > > > > Thanks for your help. > > > > John Grimes > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.