I have an update form in my web application and I'm having trouble when permitting the params I receive from this form. This is how the params gets to the controller:
<ActionController::Parameters {"company"=>{"description"=>"description",
"email_address"=>"contact@gmail.com",
"hours"=>"{\"fri\":[],\"mon\":[[\"09:00\",\"16:00\"]],\"sat\":[],
\"sun\":[[\"09:00\",\"16:00\"]],
\"thu\":[[\"09:00\",\"12:00\"], [\"13:00\",\"16:00\"]],
\"tue\":[[\"08:45\",\"16:00\"]],\"wed\":[[\"09:00\",\"16:00\"]]}"},
"controller"=>"my_controller", "action"=>"update"} permitted: false>
They all get to the controller as strings but hours is parsed to json (using JSON.parse
) resulting in something like:
{"fri"=>[], "mon"=>[["09:00", "16:00"]], "sat"=>[], "sun"=>[["09:00", "16:00"]],
"thu"=>[["09:00", "12:00"], ["13:00", "16:00"]], "tue"=>[["08:45", "16:00"]],
"wed"=>[["09:00", "16:00"]]}
Hours represents the opening hours of a store for example. It's separated by the days of the weeks. And each day can have no values for a day, have one value or two. After it's parsed hours is a hash of arrays and these arrays can be empty or has one or two arrays.
My problem is permitting this hour parameter, when using params.require(:company).permit()
. I tried many different ways like:
params.require(:company).permit(:description, : email_address, :hours)
params.require(:company).permit(:description, : email_address, hours: {})
params.require(:company).permit(:description, : email_address, hours: [])
params.require(:company).permit(:description, : email_address, hours: { [sun: [], mon: [], tue: [], wed: [], thu: [], fri: [], sat: [] })
params.require(:company).permit(:description, : email_address, hours: [ [sun: [], mon: [], tue: [], wed: [], thu: [], fri: [], sat: [] ])
- And others
but it fails or just permit it without any content, like that:
<ActionController::Parameters {"description"=>"description",
"email_address"=>"contact@gmail.com",
"hours"=><ActionController::Parameters {"sun"=>[], "mon"=>[], "tue"=>[],
"wed"=>[], "thu"=>[], "fri"=>[], "sat"=>[]} permitted: true>} permitted: true>
Thanks!