Note that there are some explanatory texts on larger screens.

plurals
  1. PORails - Dynamically build deeply nested objects (Cocoon / nested_form)
    primarykey
    data
    text
    <p>I currently have a complex form with deep nesting, and I am using the <a href="https://github.com/nathanvda/cocoon">Cocoon</a> gem to dynamically add sections as required (e.g. if a user wants to add another vehicle to the sale form). Code looks like this:</p> <pre><code>&lt;%= sale.fields_for :sale_vehicles do |sale_vehicles_builder| %&gt; &lt;%= render :partial =&gt; "sale_vehicles/form", :locals =&gt; {:f =&gt; sale_vehicles_builder, :form_actions_visible =&gt; false} %&gt; &lt;% end -%&gt; &lt;div class="add-field-links"&gt; &lt;%= link_to_add_association '&lt;i&gt;&lt;/i&gt; Add Vehicle'.html_safe, sale, :sale_vehicles, :partial =&gt; 'sale_vehicles/form', :render_options =&gt; {:locals =&gt; {:form_actions_visible =&gt; 'false', :show_features =&gt; true, :fieldset_label =&gt; 'Vehicle Details'}}, :class =&gt; 'btn' %&gt; &lt;/div&gt; </code></pre> <p>This works very nicely for the first level of nesting - the <code>sale_vehicle</code> object is built correctly by Cocoon, and the form renders as expected.</p> <p>The problem comes when there is another level of nesting - the <code>sale_vehicle</code> partial looks like this:</p> <pre><code>&lt;%= f.fields_for :vehicle do |vehicle_builder| %&gt; &lt;%= render :partial =&gt; "vehicles/form", :locals =&gt; {:f =&gt; vehicle_builder, :f_parent =&gt; f, :form_actions_visible =&gt; false, :show_features =&gt; true, :fieldset_label =&gt; 'Vehicle Details'} %&gt; &lt;% end -%&gt; </code></pre> <p>The partial for the <code>vehicle</code> is rendered with no fields, because no <code>sale_vehicle.vehicle</code> object has been built.</p> <p>What I need to do is thus build the nested object along with the main object (Cocoon does not currently build any nested objects), but how best to do this? Is there any way to select nested forms from the helper code so that these can be built?</p> <p>Cocoon currently build the main object like this:</p> <pre><code>if instance.collection? f.object.send(association).build else f.object.send("build_#{association}") end </code></pre> <p>If I could do something like the following, it would keep things nice and simple, but I'm not sure how to get <code>f.children</code> - is there any way to access nested form builders from the parent form builder?</p> <pre><code>f.children.each do |child| child.object.build end </code></pre> <p>Any help appreciated to get this working, or suggest another way of building these objects dynamically.</p> <p>Thanks!</p> <p>EDIT: Probably worth mentioning that this question appears to be relevant to both the Cocoon gem mentioned above, and also Ryan Bates' <a href="https://github.com/ryanb/nested_form">nested_form</a> gem. <a href="https://github.com/nathanvda/cocoon/issues/91">Issue #91</a> for the Cocoon gem appears to be the same problem as this one, but the workaround suggested by dnagir (delegating the building of the objects) is not ideal in this situation, as that will cause issues on other forms.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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