This project is archived and is in readonly mode.
Params hash is incorrect for nested arrays
-
David Röthlisberger
Patch with a unit test demonstrating this nested array behaviour.
-
Jeremy Kemper
- State changed from new to invalid
-
Jeremy Kemper
There's nothing to distinguish which hash keys belong to which element of the array. Looks like you expect the parser to 'remember' previously-seen hash keys and start on a new array element when a key is repeated (?)
-
David Röthlisberger
Exactly. After all, it seems like it 'remembers' previous keys when there no nested arrays:
<input name="telephone[][area_code]" ... /> <input name="telephone[][number]" ... /> <input name="telephone[][area_code]" ... /> <input name="telephone[][number]" ... />correctly gives:
"telephone" => [ { "area_code" => "nnn", "number" => "nnn" }, { "area_code" => "nnn", "number" => "nnn" } ]It seems to me that the parser is adding elements to the same hash (telephone[0]) until it sees an element with a key that is already present in the hash; then it creates a new hash (telephone[1]).
Why should the behaviour be different simply because "telephone" is nested inside an array? (I mean "why" not as in technical reasons, but as in a user's expectations). At least in there case where I am fully specifying all parameters for each record, in the correct order (i.e. there are no "sparse" records).
The whole parser is quite confusing and I am not the first to expect it to behave differently :-)
See e.g. http://dev.rubyonrails.org/ticke...:11
But I am happy with closing this; I have since learnt that the best way to achieve this is with the :index option to forms_for and fields_for, which over-rides the default behaviour of using the record's database id as the key in the params hashes.
-
Brad Gessler
- Tag set to actionpack, bug
I've had this exact same problem. Rails seems to only parse two levels deep because it doesn't remember the previously seen hash-keys.
Don't be so quick to close the issue. It looks like they're getting there with #474 Allow Hash to be passed into AssociationProxy setters and AssociationCollections for updates and creations; but I didn't see any activity on the params parser for supporting deep model saves.