'How to permit a hash with id keys in params?
Having this kind of params where 14 and 5 are keys. They are not randomly generated. Those are payment_ids(ask you can see).
<ActionController::Parameters {"14"=>{"customer_id"=>"28", "payment_id"=>"14", "plan_id"=>"121"}, "5"=>{"customer_id"=>"28", "payment_id"=>"5", "plan_id"=>"7"}} permitted: false>
So my question would be how correctly permit params?
I understand that I need to do something like this but in a current way "14" is hardcoded and static
params.require(:param_name).permit("14" => {})
Solution 1:[1]
This seems to be working given your example:
# Find the keys that are 1-2 digit numbers
numeric_param_keys = params.keys.select { |key| key =~ /\A\d{1,2}\z/ }
# Create an array of hashes from those numeric keys
permissions = numeric_param_keys.map { |key| { key => {} } }
params.permit(permissions)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
