Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As others have mentioned, the <code>[]</code> should contain a key for new records because otherwise it is mixing a hash with an array type. You can set this with the <code>child_index</code> option on fields_for.</p> <pre><code>f.fields_for :items, Item.new, child_index: "NEW_ITEM" # ... </code></pre> <p>I usually do this using the <code>object_id</code> instead to ensure it is unique in case there are multiple new items.</p> <pre><code>item = Item.new f.fields_for :items, item, child_index: item.object_id # ... </code></pre> <p>Here's an abstract helper method that does this. This assumes there is a partial with the name of <code>item_fields</code> which it will render.</p> <pre><code>def link_to_add_fields(name, f, association) new_object = f.object.send(association).klass.new id = new_object.object_id fields = f.fields_for(association, new_object, child_index: id) do |builder| render(association.to_s.singularize + "_fields", f: builder) end link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) end </code></pre> <p>You can use it like this. The arguments are: the name of the link, the parent's form builder, and the name of the association on the parent model.</p> <pre><code>&lt;%= link_to_add_fields "Add Item", f, :items %&gt; </code></pre> <p>And here is some CoffeeScript to listen to the click event of that link, insert the fields, and update the object id with the current time to give it a unique key.</p> <pre><code>jQuery -&gt; $('form').on 'click', '.add_fields', (event) -&gt; time = new Date().getTime() regexp = new RegExp($(this).data('id'), 'g') $(this).before($(this).data('fields').replace(regexp, time)) event.preventDefault() </code></pre> <p>That code is taken from <a href="http://railscasts.com/episodes/196-nested-model-form-revised" rel="noreferrer">this RailsCasts Pro episode</a> which requires a paid subscription. However, there is a full working example freely available <a href="https://github.com/railscasts/196-nested-model-form-revised" rel="noreferrer">on GitHub</a>.</p> <p><strong>Update:</strong> I want to point out that inserting a <code>child_index</code> placeholder is not always necessary. If you do not want to use JavaScript to insert new records dynamically, you can build them up ahead of time:</p> <pre><code>def new @project = Project.new 3.times { @project.items.build } end &lt;%= f.fields_for :items do |builder| %&gt; </code></pre> <p>Rails will automatically insert an index for the new records so it should just work.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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