# Nested forms in rails 2.3

**URL:** <https://www.ruby-forum.com/t/nested-forms-in-rails-2-3/162991>\
**Category:** Rails\
**Created:** [March 16, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/nested-forms-in-rails-2-3/162991 "2009-03-16T13:32:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tilt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tilt](https://www.ruby-forum.com/u/tilt)\
**Post date:** [March 16, 2009, 1:32pm UTC](https://www.ruby-forum.com/t/nested-forms-in-rails-2-3/162991/1 "2009-03-16T13:32:51Z")

</div>

Hi,

Is there a simpler way to implement the view-component of nested forms  
as it was done in Railscast #75 (Complex Forms Part 3) in Rails 2.3?

I’d like to add and remove the owned objects dynamically using AJAX  
requests, but using f.fields\_for :object do |object\_form| doen’t let  
me load new objects into the form - or at least I don’t fully  
understand how that should work.

Should I use fileds\_for :object, iterator do |object\_form instead?  
Does anyone know a simple example for this kind of usage on the web?

Thanks for any advice!

Best regards

---

<div class="post-metadata">

**Author:** ![tilt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tilt](https://www.ruby-forum.com/u/tilt)\
**Post date:** [March 16, 2009, 3:51pm UTC](https://www.ruby-forum.com/t/nested-forms-in-rails-2-3/162991/2 "2009-03-16T15:51:12Z")

</div>

On Mar 16, 12:32 pm, tilt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’d like to add and remove the owned objects dynamically using AJAX  
> requests, but using f.fields\_for :object do |object\_form| doen’t let  
> me load new objects into the form - or at least I don’t fully  
> understand how that should work.
> 
> Should I use fileds\_for :object, iterator do |object\_form instead?  
> Does anyone know a simple example for this kind of usage on the web?

I’m not sure exactly which bit you are stuck at but any modifications  
to the object association should be represented by fields\_for. As one  
way you can get a new object template is to simply build a blank one  
on the collection, e.g:  
@parent = Parent.find(…)  
@parent.assoc\_objects.build  
form\_for @parent… do |f|  
f.fields\_for :assoc\_objects do…  
# Will have ‘empty’ here

Hope that helps.

Andrew

---

<div class="post-metadata">

**Author:** ![tilt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tilt](https://www.ruby-forum.com/u/tilt)\
**Post date:** [March 17, 2009, 2:07pm UTC](https://www.ruby-forum.com/t/nested-forms-in-rails-2-3/162991/3 "2009-03-17T14:07:08Z")

</div>

Hi,

Thanks for your answer!

Sorry, I mixed some things up in my question above.

I’d like the associated objects to be chosen from a select box using  
collection\_select. The request data submitted to update the associated  
objects is:

object=\>{ … “associated\_object\_attributes”=\>{“0”=\>{"\_delete"=\>“0”,  
“id”=\>“1”}, “1”=\>{"\_delete"=\>“0”, “id”=\>“3”}, “2”=\>{“id”=\>“3”}} }

So only the IDs are submitted. I seemingly have to handle this in the  
controller by myself because the other data of the associated objects  
is missing.

If I’m wrong I’d be very thankful if someone corrected me.

Best regards
