title: How to submit multiple NEW items via Rails 3.2 mass-assignment
body: <p>I have a pretty standard use-case. I have a parent object and a list of child objects. I want to have a tabular form where I can edit all the children at once, as rows in the table. I also want to be able to insert one or more <em>new</em> rows, and on submit have them be created as new records.</p>
  
  <p>When I use a <code>fields_for</code> to render a series of sub-forms for nested records related by has-many, rails generates field names e.g. <code>parent[children_attributes][0][fieldname]</code>, <code>parent[children_attributes][1][fieldname]</code> and so on.</p>
  
  <p>This causes Rack to parse a params hash that looks like:</p>
  
  <pre><code>{ "parent" =&gt; { 
      "children" =&gt; {
        "0" =&gt; { ... },
        "1" =&gt; { ... } } }
  </code></pre>
  
  <p>When passed a <em>new</em> (un-persisted) object, the same <code>fields_for</code> will generate a field name that looks like:</p>
  
  <pre><code>parent[children_attributes][][fieldname]
  </code></pre>
  
  <p>Note the <code>[]</code> with no index in it.</p>
  
  <p>This <em>cannot</em> be posted in the same form with the fields containing <code>[0]</code>, <code>[1]</code>, etc. because Rack gets confused and raises </p>
  
  <pre><code>TypeError: expected Array (got Rack::Utils::KeySpaceConstrainedParams)
  </code></pre>
  
  <p>"OK", thinks I. "I'll just make sure <em>all</em> the fields use the <code>[]</code> form instead of the <code>[index]</code> form. But I can't figure out how to convince <code>fields_for</code> to do this consistently. Even if I give it an explicit field name prefix and object:</p>
  
  <pre><code>fields_for 'parent[children_attributes][]', child do |f| ...
  </code></pre>
  
  <p>So long as <code>child</code> is persisted, it will automatically modify the fieldnames so that they become e.g. <code>parent[children_attributes][0][fieldname]</code>, while leaving fieldnames for new records as <code>parent[children_attributes][][fieldname]</code>. Once again, Rack barfs.</p>
  
  <p>I'm at a loss. How the heck do I use standard Rails helpers like <code>fields_for</code> to submit multiple <em>new</em> records, along with existing records, have them be parsed as an array in the params, and have all the records lacking IDs be created as new records in the DB? Am I out of luck and I just have to generate all the field names manually?</p>