I have the following params:
params={"data"=>
{"type"=>"book",
"id"=>14,
"attributes"=>
{"id"=>14,
"created_at"=>"2022-06-27 21:15:39",
"title"=>"sdfdsf",
"targeting"=> { "release_times"=>[["4:00", "5:00"], ["5:00", "6:00"]],
"days"=>["Monday", "Tuesday", "Wednesday"],
"gender"=>["male", "female"]
}
}
}
When I use this, I can get every value but release_times is always null:
When I use this:
safe_params = params.require(:data).permit( attributes: [:id, :created_at, :title, { targeting: {} }])
How can I extract the release times value? I tried doing this
safe_params = params.require(:data).permit( attributes: [:id, :created_at, :title, { targeting: [:days, :gender, release_times:[]]}])
But I get the error:
Validation failed: Targeting gender should be a list of values, Targeting days should be a list of values
How can I extract all the values from targeting including the release_times?
As Ruby on Rails API states, when using
ActionController::Parametersyou want to declare that a parameter should be an array (list) by mapping it to a empty array. Like you did withrelease_times.You should permit
targetingparams with[days: [], gender: []]instead of[:days, :gender]. This should solve the error.But even them,
release_timesis an array of arrays, which I believe is not supported at the moment (there is an old issue for it).One way you could bypass this would be by changing the way you're communicating
release_times. Using an arrays of hashes instead of nested arrays.From this:
To this (or something similar):
That way, you could do this:
Exactly how you would implement that is up to you, but I hope it helps.
**Also, there was a typo with release_times.
You can do some testing yourself. Open
rails cand do something like this:Just an observation, using
permit!would work. But as strong params doc says:So you could try to
slicearguments yourself and thempermit!- but I can't tell you that's the way to go.Learn more about Mass Assignment Vulnerability here.