Forum: Ruby on Rails nested forms in rails 2.3

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
tilt (Guest)
on 2009-03-16 14:32
(Received via mailing list)
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
Andrew F. (Guest)
on 2009-03-16 16:51
(Received via mailing list)
On Mar 16, 12:32 pm, tilt <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
tilt (Guest)
on 2009-03-17 15:07
(Received via mailing list)
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
This topic is locked and can not be replied to.