简体   繁体   中英

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" => {})

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)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM