Reputation: 2457
Okay, I've been struggling with this for a while now and I'm not sure what i'm doing wrong. I know this has been asked a couple of times but the answers in the other questions are not quite working on my case. I'm trying to send a specific field as an array to rails to no avail.
The generated html looks like this
<input class="form-control" id="groups_header_0_version_number_value" name="groups[header][0][version][number][value]" placeholder="value" type="text">
<input class="form-control" id="groups_header_0_version_creator_value" name="groups[header][0][version][creator][value]" placeholder="value" type="text">
<input class="form-control" id="groups_header_1_version_number_value" name="groups[header][1][version][number][value]" placeholder="value" type="text">
<input class="form-control" id="groups_header_1_version_creator_value" name="groups[header][1][version][creator][value]" placeholder="value" type="text">
I want to send the header
as an array to rails server. When I render params[:group]
in my controller as json
I get
{
"header":{
"0":{
"version":{
"number":{
"operator":"\u003c=",
"value":"34"
},
"creator":{
"operator":"=",
"value":"joseph"
}
}
},
"1":{
"version":{
"number":{
"operator":"\u003c",
"value":"87"
},
"creator":{
"operator":"=",
"value":"john"
}
}
}
}
}
What I really want is a json structure with header
as an array like
{
"header":[
{
"0":{
"version":{
"number":{
"operator":"\u003c=",
"value":"34"
},
"creator":{
"operator":"=",
"value":"joseph"
}
}
},
"1":{
"version":{
"number":{
"operator":"\u003c",
"value":"87"
},
"creator":{
"operator":"=",
"value":"john"
}
}
}
}
]
}
How should I correctly name the input boxes to get params sent as above? Thanks
Upvotes: 0
Views: 57
Reputation: 8846
You are going to want to change your name attribute to:
<input ... name="groups[header][][0][version][number][value]" ... >
<input ... name="groups[header][][0][version][creator][value]" ... >
<input ... name="groups[header][][1][version][number][value]" ... >
<input ... name="groups[header][][1][version][creator][value]" ... >
Notice the extra []
after [header]
This produces the params
"groups"=>{"header"=>
[
{
"0"=>{"version"=>{"number"=>{"value"=>""}, "creator"=>{"value"=>""}}},
"1"=>{"version"=>{"number"=>{"value"=>""}, "creator"=>{"value"=>""}}}
}
]
}
which appears to be what you want.
Upvotes: 1