kwyoung11
kwyoung11

Reputation: 978

Rails nested parameter parsing

I've got a model Klass, which can have several Note objects. In my form I have the field name as klass[notes][][note_ids][].

Now, if I pass the following parameters into the request:

klass[notes][][note_ids][]=5cb95789-6c35-4d39-aa17-c3ddbbbcf540&klass[notes][][note_ids][]=2cfb26df-21b4-43d7-aa65-735569d42ad9

They get parsed as follows (using Rack::Utils.parse_nested_query):

{"klass"=>{"notes"=>[{"note_ids"=>["5cb95789-6c35-4d39-aa17-c3ddbbbcf540", "2cfb26df-21b4-43d7-aa65-735569d42ad9"]}]}}

How do I make it so that they instead get parsed into the following:

{"klass"=>{"notes"=>[{"note_ids"=>["5cb95789-6c35-4d39-aa17-c3ddbbbcf540"]}, {"note_ids"=>["2cfb26df-21b4-43d7-aa65-735569d42ad9"]}]}}

Upvotes: 0

Views: 245

Answers (1)

Anuj
Anuj

Reputation: 1940

With the current scheme, it will be impossible for the parser to tell if the two ids are to be grouped at the note_ids level or at the notes level. It groups them at the first valid level it finds.

You could help the parser by grouping the outer level manually. For example,

klass[notes][0][note_ids][]=1&klass[notes][1][note_ids][]=2

gets parsed as

 {"klass"=>{"notes"=>{"0"=>{"note_ids"=>["1"]}, "1"=>{"note_ids"=>["2"]}}}}

Upvotes: 1

Related Questions