# Nested forms: Can you access the object from within fields\_for?

**URL:** <https://www.ruby-forum.com/t/nested-forms-can-you-access-the-object-from-within-fields-for/186716>\
**Category:** Rails\
**Created:** [April 12, 2010, 4:15am UTC](https://www.ruby-forum.com/t/nested-forms-can-you-access-the-object-from-within-fields-for/186716 "2010-04-12T04:15:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Grimes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7cd45c/32.png) [@John\_Grimes](https://www.ruby-forum.com/u/John_Grimes)\
**Post date:** [April 12, 2010, 4:15am UTC](https://www.ruby-forum.com/t/nested-forms-can-you-access-the-object-from-within-fields-for/186716/1 "2010-04-12T04:15:04Z")

</div>

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

\<%= f.submit "Submit" %\>

\<% 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| %\>  
\<%= friend.name %\>  
\<%= friends.text\_field :relationship\_to\_you %\>  
\<% end %\>

\<%= f.submit "Submit" %\>

\<% end %\>

Thanks for your help.

John Grimes

---

<div class="post-metadata">

**Author:** ![John\_Grimes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7cd45c/32.png) [@John\_Grimes](https://www.ruby-forum.com/u/John_Grimes)\
**Post date:** [April 12, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/nested-forms-can-you-access-the-object-from-within-fields-for/186716/2 "2010-04-12T14:32:41Z")

</div>

Yes u can with  
friend.object.name

---

<div class="post-metadata">

**Author:** ![John\_Grimes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7cd45c/32.png) [@John\_Grimes](https://www.ruby-forum.com/u/John_Grimes)\
**Post date:** [April 13, 2010, 5:50am UTC](https://www.ruby-forum.com/t/nested-forms-can-you-access-the-object-from-within-fields-for/186716/3 "2010-04-13T05:50:41Z")

</div>

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| %\>  
\<%= friends.object.name [http://friend.name](http://friend.name) %\>  
\<%= friends.text\_field :relationship\_to\_you %\>  
\<% end %\>

\<%= f.submit "Submit" %\>

\<% end %\>
