In one of my controllers I have the line:
params.require(:ad).permit(permitted_array)
params[:ad] is a hash. When permitted keys are present in this hash, this line works fine. When params[:ad] is an empty hash, the above line fails.
Example: With params being
{"ad"=>{}, "controller"=>"ads", "action"=>"create"}
I get this error after params.require(:ad) is called:
ActionController::ParameterMissing:
   param is missing or the value is empty: advertiser
But this works fine:
[2] pry()> params
=> {"ad"=>{"name"=>"kjj", "ad_type_id"=>1, "fee"=>"9", "click_attr"=>"8", "imp_attr"=>"7"}, "controller"=>"ads", "action"=>"create"}
[3] pry()> params.require(:ad).permit(permitted_array)
=> {"name"=>"kjj", "ad_type_id"=>1, "fee"=>"9", "click_attr"=>"8", "imp_attr"=>"7"}
How can I get this line to accept an empty hash?
Make the params require conditional, require the key only if it is present, like this:
params.require(:ad).permit(permitted_array) if params[:ad].present?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With