0

I am having issues permitting all on one of the sub-hashes. How could I do this in strong_params in Rails 4?

    params.require(:checkout_item).permit(:checkout_group_transaction_id, :item_type, :property_id,
                                          :image)
    params.require(:checkout_item[:data]).permit!

Where :checkout_item[:data] should be permit all ??

Kamilski81
  • 14,409
  • 33
  • 108
  • 161

1 Answers1

1

What you want to do is whitelist every key in params[:checkout_item][:data], which is a hash. You can use the keys method here to return an array of the keys for that hash.

params.require(:checkout_item).permit(:checkout_group_transaction_id, 
                                      :item_type, :property_id, :image,
                                      {:data => params[:checkout_item][:data].try(:keys)})

Now every key in params[:checkout_item][:data] will be permitted regardless of how many keys appear for a given request. Note the use of try to avoid raising a NoMethodError exception on params[:checkout_item][:data] in the event that it is nil since you may not need to pass the :data sub hash on every request to create or update.

I took this idea from the action controller rails guide section 4.5.4 Outside the Scope of Strong Parameters.

Hope this helps!

dave_slash_null
  • 1,124
  • 7
  • 16