views:

708

answers:

4

Hi folks,

I've the following code in a _form.html.haml partial, it's used for new and edit actions. (fyi I use the Ryan Bates' plugin nested_form)

.fields
    - f.fields_for :transportations do |builder|
        = builder.collection_select :person_id, @people, :id, :name, {:multiple => true}
        = builder.link_to_remove 'effacer'
    = f.link_to_add "ajouter", :transportations

works fine for the new action... for the edit action, as explain in the doc, I've to add the :id of already existing associations, so, I've to add something like

= builder.hidden_field :id, ?the value? if ?.new_record?

How can I get the value?

Here is the doc of accepts_nested_attributes_for for reference (source: http://github.com/rails/rails/blob/master/activerecord/lib/active_record/nested_attributes.rb#L332)

# Assigns the given attributes to the collection association.
#
# Hashes with an <tt>:id</tt> value matching an existing associated record
# will update that record. Hashes without an <tt>:id</tt> value will build
# a new record for the association. Hashes with a matching <tt>:id</tt>
# value and a <tt>:_destroy</tt> key set to a truthy value will mark the
# matched record for destruction.
#
# For example:
#
# assign_nested_attributes_for_collection_association(:people, {
# '1' => { :id => '1', :name => 'Peter' },
# '2' => { :name => 'John' },
# '3' => { :id => '2', :_destroy => true }
# })
#
# Will update the name of the Person with ID 1, build a new associated
# person with the name `John', and mark the associatied Person with ID 2
# for destruction.
#
# Also accepts an Array of attribute hashes:
#
# assign_nested_attributes_for_collection_association(:people, [
# { :id => '1', :name => 'Peter' },
# { :name => 'John' },
# { :id => '2', :_destroy => true }
# ])

Thanks for your help.

A: 

Nested forms are officially supported with Rails. What you are doing (specifically with the fields_for method) may be conflicting with RAils' built-in way to render fields_for.

Here's the documentation for the Rails way to do fields_for...it's very thorough:

http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001605

I highly recommend you try the built-in way instead of the plugin, as that will continue to be supported almost indefinitely.

Hope this helps!

btelles
Thanks for your answer. I do use the built-in way by using accepts_nested_attributes_for. Using the plugin provide me facilities for remove and add link only. My question is completely related to the use of the built-way. As explained in the accepts_nested_attributes_for doc, I've to provide an :id value matching an existing associated record, this way the built-in functionality will update that record automatically. Cheers
denisjacquemin
A: 

Hey,

I found my error, here is what i learned fyi:

When you use accepts_nested_attributes_for with many to many associations, keep the :id primary key for the association table.

Cheers

denisjacquemin
+1  A: 

Denis - I think I'm having the same problem. My nested forms worked until I threw in that ":_destroy" method... what do you mean "keep the :id primary"?

KT
What version of rails do you use, I know that the _destroy argument is valid from version 2.3.5, before it was _delete.
denisjacquemin
A: 

Mine works when using ":_delete" instead of ":_destroy". I am on rails 2.3.4. Ruby 1.8.7

Check out this: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001605

Jay Zheng
yes, the _destroy argument is valid from version 2.3.5, before it was _delete, cheers
denisjacquemin