I have a params hash with the following structure (it was built using fields_for)
=> {"utf8"=>"✓",
"daily_log"=>
{"id"=>"1",
"entries_attributes"=>
{"0"=>
{"count"=>"",
"hours"=>"",
"minutes"=>"",
}
}
},
"controller"=>"entries",
"action"=>"create"}
I'm trying to create a an entries_attributes_params
method which should just return a whitelisted version of params['daily_log']['entries_attributes']
. Unfortunately, this keeps coming back as {}
when I call the method.
The following works
def entries_attributes_params`
params[:daily_log][:entries_attributes].permit!
end
But i want to avoid using permit!
. So I tried the following:
def entries_attributes_params
params[:daily_log][:entries_attributes].permit(:count, :hours, :minutes)
end
This doesn't work. I get {}
back.
I tried wrapping the attributes I pass into permit
with []
, but that doesn't work.
...permit([...attributes...])
How do I do this?
Try this:
def entries_attributes_params
params_hash = params.require(:daily_log).permit(:id, entries_attributes: [:count, :hours, :minutes])
params_hash[:entries_attributes]
end
Figured it out. The key is to recognize that params.permit ...
doesn't actually change the main params hash. It creates a new object. Combining this idea with KM's solution below gives the full answer:
def entries_attributes_params
p = params.require(:daily_log).permit(:id, entries_attributes: [:count, :hours, :minutes])
p[:entries_attributes]
end
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.