# Fields\_for

**URL:** <https://www.ruby-forum.com/t/fields-for/181332>\
**Category:** Rails\
**Created:** [January 9, 2010, 11:41pm UTC](https://www.ruby-forum.com/t/fields-for/181332 "2010-01-09T23:41:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 9, 2010, 11:41pm UTC](https://www.ruby-forum.com/t/fields-for/181332/1 "2010-01-09T23:41:59Z")

</div>

I need to display information from the collection in a fields\_for  
without using the fields\_for block’s object. For example, in the  
following code:

\<% form\_for :my\_object, @my\_object, :url =\> { :action =\> “my\_action” }  
do |form| %\>  
Last name: \<%= form.text\_field :last %\>  
\<% from.fields\_for :my\_collection do |fields\_for\_object| %\>

```
    <%= fields_for_object.text_field :my_attribute %> # <<<<< This

```

is the line I’m having trouble with.

```
<% end -%>
<%= submit_tag 'Save' %>

```

\<% end %\>

I would like to replace:  
\<%= fields\_for\_object.text\_field :my\_attribute %\>

With simply:  
\<%= @an\_object.my\_attribute %\>

Basically I just want to display an attribute that belongs to the  
collection object being processed as text, with no ‘input’ box or  
anything else around it. I have looked and looked but couldn’t find an  
example. Is this possible?

Thank you.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 9, 2010, 11:50pm UTC](https://www.ruby-forum.com/t/fields-for/181332/2 "2010-01-09T23:50:46Z")

</div>

Sorry, there was a typo: \<% from.fields\_for :my\_collection do |  
fields\_for\_object| %\> should have been \<%  
form.fields\_for :my\_collection do |fields\_for\_object| %\>. Also, after  
thinking about it, what I am really looking for is access to the  
collection object being processed, not just one of it’s attributes.

Any help would be much appreciated.

Thank you.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 10, 2010, 1:00am UTC](https://www.ruby-forum.com/t/fields-for/181332/3 "2010-01-10T01:00:10Z")

</div>

pepe wrote:

> Sorry, there was a typo: \<% from.fields\_for :my\_collection do |  
> fields\_for\_object| %\> should have been \<%  
> form.fields\_for :my\_collection do |fields\_for\_object| %\>. Also, after  
> thinking about it, what I am really looking for is access to the  
> collection object being processed, not just one of it’s attributes.

Then why don’t you just call methods on @my\_object.my\_collection ? It  
sounds like you don’t need fields\_for.

> Any help would be much appreciated.
> 
> Thank you.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 10, 2010, 3:21am UTC](https://www.ruby-forum.com/t/fields-for/181332/4 "2010-01-10T03:21:25Z")

</div>

Marnen,

I do need fields\_for, I am getting at data 2 levels deep in  
associations (main table + association + association) and I need to  
update the contents of the associations.

I am just learning how to use ‘fields\_for’ so forgive me if I sound  
like I don’t know what I’m talking about.

My understanding is that ‘fields\_for’ will iterate through the  
collection, hence working with one element/object of the collection in  
each iteration. If I need an attribute from the element/object at each  
iteration, how would I know which element to access with  
@my\_object.my\_collection? I don’t even know if there is an index I can  
use to get to the element I need as in @my\_object.my\_collection  
[my\_index], hence allowing me to do something like:

\<%= @my\_object.my\_collection[my\_index].my\_attribute %\>

In any case, I found a line of code “hidden” in the documentation that  
gave me a clue to find what I needed:

\<% if project\_fields.object.active? %\>

As it turns out you can access the object that ‘fields\_for’ is using  
by just using method ‘object’ on the block object (“fieds\_for\_object”  
in the sample below):

\<% form\_for :my\_object, @my\_object, :url =\> { :action =\> “my\_action” }  
do |form| %\>  
\<% form.fields\_for :my\_collection do |fields\_for\_object| %\>  
\<%= fields\_for\_object.object.my\_attribute %\> # \<\<\< This is  
what I was looking for

Thanks for the insight, though.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 10, 2010, 4:45am UTC](https://www.ruby-forum.com/t/fields-for/181332/5 "2010-01-10T04:45:42Z")

</div>

On Sat, Jan 9, 2010 at 9:19 PM, pepe [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am just learning how to use ‘fields\_for’ so forgive me if I sound  
> like I don’t know what I’m talking about.
> 
> My understanding is that ‘fields\_for’ will iterate through the  
> collection, hence working with one element/object of the collection in  
> each iteration. If I need an attribute from the element/object at each  
> iteration, how would I know which element to access with  
> @my\_object.my\_collection? I don’t even know if there is an index I can  
> use to get to the element I need as in @my\_object.my\_collection  
> [my\_index], hence allowing me to do something like:

No fields\_for doesn’t do any iteration. It’s used for setting up a  
scope for one or more form fields for a different object than the one  
which scopes an outer form\_for

From the doc:

\<% form\_for @person, :url =\> { :action =\> “update” } do |person\_form| %\>  
First name: \<%= person\_form.text\_field :first\_name %\>  
Last name : \<%= person\_form.text\_field :last\_name %\>

```
<% fields_for :person do |permission_fields| %>
   Admin?: <%= permission_fields.check_box :admin %>
<% end %>

```

\<% end %\>

So just like form\_for doesn’t do any iteration, neither does fields\_for.

Since you haven’t told us any more about what you want to do, I don’t  
know how to help any further.

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 10, 2010, 3:41pm UTC](https://www.ruby-forum.com/t/fields-for/181332/6 "2010-01-10T15:41:05Z")

</div>

Thanks Rick,

After I read your post and looked for the nth time at the  
documentation I finally “saw” a couple of things that I had completely  
misunderstood. I still need help, though. Iâ€™ll try to explain as best  
as possible.

- The application uses a legacy DB
- A person has many person\_projects (1 to n)
- A person\_project has one project
- A person\_project has one supervisor
- A project contains a group code (this is important, the projects are  
selected by the group code)
- A project has one supervisor (1)
- A project has many contractors (0 to n)
- A contractor has one supervisor (1)
- A supervisor is not a person, meaning his/her data is not in the  
same table as a personâ€™s data (remember, this is a legacy DB)
- The application is â€œwizardâ€ style and the same pages are used for  
create/update
- The information should not be changed/created until the last page of  
the â€œwizardâ€ is hit, which fortunately is the page used to select the  
information for the person\_project records

In a first page the user selects the group codes and that list will  
provide all the projects a person should have person\_projects for.  
Once the group codes are selected the next page should display a table/  
list of the projects. If the person record already exists this list  
might match exactly the current person\_projects list and/or contain  
different projects and/orâ€¦ every possible combination. In addition to  
that for each project there should be a drop down of supervisors to  
choose from composed of:

- The current person\_project supervisor if the person\_project record  
exists
- The project supervisor (the supervisor ID stored in the  
person\_project record could be different [wrong or most likely empty]  
than what the project record indicates should be)
- All the supervisors for the project contractors

The person\_project table should be updated by:

- Removing any record from the person\_projects table not on the  
selected list
- Updating the signatory IDs in those existing person\_project records  
where they have changed
- Adding new records for any projects not yet in the person\_project  
table

Unfortunately I am not as comfortable with Rails yet as I would like  
to be and I have been working on this on and off for more than 2 weeks  
now. I have hit every possible page (many times) I could find with  
information about complex forms and multi record processing, watched  
the railscasts, hit the books, everythingâ€¦ Depending on the approach I  
take sometimes validations are skipped or new person\_project records  
are not saved, orâ€¦ Iâ€™ve hit every possible roadblock.

If you guys know how to solve this riddle I would be very, very  
thankful.

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 10, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/fields-for/181332/7 "2010-01-10T17:19:16Z")

</div>

pepe wrote:  
[…]

> Unfortunately I am not as comfortable with Rails yet as I would like  
> to be and I have been working on this on and off for more than 2 weeks  
> now. I have hit every possible page (many times) I could find with  
> information about complex forms and multi record processing, watched  
> the railscasts, hit the books, everythingâ€¦ Depending on the approach I  
> take sometimes validations are skipped or new person\_project records  
> are not saved, orâ€¦ Iâ€™ve hit every possible roadblock.

Which tests are failing under which circumstances?

> If you guys know how to solve this riddle I would be very, very  
> thankful.
> 
> Thanks a lot.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![pepe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4da419/32.png) [@pepe](https://www.ruby-forum.com/u/pepe)\
**Post date:** [January 10, 2010, 7:31pm UTC](https://www.ruby-forum.com/t/fields-for/181332/8 "2010-01-10T19:31:06Z")

</div>

Hi Marnen,

Thanks for taking an interest. I really appreciate you and Rick taking  
the time to check on my problem.

I finally made the whole thing work (kind of). I know I am doing  
something wrong because the associated objects validations are not  
being hit, probably because I am creating the wrong type of object,  
(‘project’ rather than ‘person\_project’) for the form as you can see  
in the params hash below, but at this point I am already too tired of  
the problem and I am also too late to keep looking into it or I won’t  
meet the deadline I have. I would like to fix it in the (near) future,  
though, so here goes some more info in case it helps.

This is a sample of the params hash (edited):

person: !map:HashWithIndifferentAccess

# person fields and values go here

person\_projects\_attributes: !map:HashWithIndifferentAccess  
“0”: !map:HashWithIndifferentAccess  
project\_attributes: !map:HashWithIndifferentAccess  
id: “000267”  
project\_id: “000267”  
supervisor\_id: “10”  
id: “10326”  
“1”: !map:HashWithIndifferentAccess  
project\_attributes: !map:HashWithIndifferentAccess  
id: 000392  
project\_id: 000392  
supervisor\_id: “2”

I ended up validating the associated objects values by hand in the  
controller. Something like this:

def validate\_person\_projects\_list  
params[:person][:person\_projects\_attributes].each\_pair do |index,  
person\_project|  
if person\_project[:supervisor\_id].blank?  
@person.errors.add\_to\_base ‘All projects must have a  
supervisor.’  
return  
end  
end  
end # def validate\_person\_projects

This is the form (edited):

\<% form\_for :person, @person, :url =\> { :action =\> “approval” } do |f|  
%\>

# person’s fields go here

```
<% for @person_project in @person.person_projects %>
  <% f.fields_for :person_projects, @person_project do |

```

pp\_fields| %\>  
\<%= pp\_fields.hidden\_field :project\_id -%\>  
\<%= pp\_fields.select :supervisor\_id,  
@person\_project.supervisors.map {|s| [s.id.to\_s + ’ -\> ’ + s.lname.strip + ', ’ + s.fname.strip, s.id]}.sort {|a, b| a[0] \<=\>  
b[0]} -%\>  
\<% end %\>  
\<% end %\>  
\<%= submit\_tag ‘Save’ -%\>  
\<% end -%\>

@person\_project#supervisors is a model method that will return the  
list of supervisor objects to choose from: current record’s, current  
record project’s and current record project contractors’.

Thanks again for taking the time. I really appreciate it.
