Nested forms in rails 2.3

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

On Mar 16, 12:32 pm, tilt [email protected] 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

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