The following before filter looks for any Hash
parameters where all the keys are integers and turns them into Array
s. It searches recursively to allow the used of nested attributes within nested attributes. This is necessary to circumvent a bug in the strong_parameters
gem which was reported in April 2012. Given params like:
{
"person" => {
"dogs_attributes" => {
"0" => {
"name" => "Fido"
},
"1" => {
"name" => "Rocket"
}
}
}
}
It will modify them to look like:
{
"person" => {
"dogs_attributes" => [
{
"name" => "Fido"
},
{
"name" => "Rocket"
}
]
}
}
Simply copy the before_filter
macro and repair_nested_params
method below into your application controller.
don't know if anyone cares at this point, but if you have a hash that needs to be fixed inside another hash that needs to be fixed (ie. two levels of nested attributes), this doesn't fix the inner level. changing line 16 to
obj[key].each {|h| repair_nested_params(h) }
fixes it