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 new rows, and on submit have them be created as new records.
When I use a fields_for
to render a series of sub-forms for nested records related by has-many, rails generates field names e.g. parent[children_attributes][0][fieldname]
, parent[children_attributes][1][fieldname]
and so on.
This causes Rack to parse a params hash that looks like:
{ "parent" => {
"children" => {
"0" => { ... },
"1" => { ... } } }
When passed a new (un-persisted) object, the same fields_for
will generate a field name that looks like:
parent[children_attributes][][fieldname]
Note the []
with no index in it.
This cannot be posted in the same form with the fields containing [0]
, [1]
, etc. because Rack gets confused and raises
TypeError: expected Array (got Rack::Utils::KeySpaceConstrainedParams)
"OK", thinks I. "I'll just make sure all the fields use the []
form instead of the [index]
form. But I can't figure out how to convince fields_for
to do this consistently. Even if I give it an explicit field name prefix and object:
fields_for 'parent[children_attributes][]', child do |f| ...
So long as child
is persisted, it will automatically modify the fieldnames so that they become e.g. parent[children_attributes][0][fieldname]
, while leaving fieldnames for new records as parent[children_attributes][][fieldname]
. Once again, Rack barfs.
I'm at a loss. How the heck do I use standard Rails helpers like fields_for
to submit multiple new 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?