Note that there are some explanatory texts on larger screens.

plurals
  1. POaccepts_nested_attributes_for and nested_form plugin
    text
    copied!<p>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 <a href="http://github.com/ryanb/nested_form" rel="nofollow noreferrer">nested_form</a>)</p> <pre><code>.fields - f.fields_for :transportations do |builder| = builder.collection_select :person_id, @people, :id, :name, {:multiple =&gt; true} = builder.link_to_remove 'effacer' = f.link_to_add "ajouter", :transportations </code></pre> <p>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</p> <pre><code>= builder.hidden_field :id, ?the value? if ?.new_record? </code></pre> <p>How can I get the value?</p> <p>Here is the doc of accepts_nested_attributes_for for reference (source: <a href="http://github.com/rails/rails/blob/master/activerecord/lib/active_record/nested_attributes.rb#L332" rel="nofollow noreferrer">http://github.com/rails/rails/blob/master/activerecord/lib/active_record/nested_attributes.rb#L332</a>)</p> <pre><code># Assigns the given attributes to the collection association. # # Hashes with an &lt;tt&gt;:id&lt;/tt&gt; value matching an existing associated record # will update that record. Hashes without an &lt;tt&gt;:id&lt;/tt&gt; value will build # a new record for the association. Hashes with a matching &lt;tt&gt;:id&lt;/tt&gt; # value and a &lt;tt&gt;:_destroy&lt;/tt&gt; key set to a truthy value will mark the # matched record for destruction. # # For example: # # assign_nested_attributes_for_collection_association(:people, { # '1' =&gt; { :id =&gt; '1', :name =&gt; 'Peter' }, # '2' =&gt; { :name =&gt; 'John' }, # '3' =&gt; { :id =&gt; '2', :_destroy =&gt; 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 =&gt; '1', :name =&gt; 'Peter' }, # { :name =&gt; 'John' }, # { :id =&gt; '2', :_destroy =&gt; true } # ]) </code></pre> <p>Thanks for your help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload