简体   繁体   English

如何允许参数中带有 id 键的 hash?

[英]How to permit a hash with id keys in params?

Having this kind of params where 14 and 5 are keys.拥有这种参数,其中 14 和 5 是键。 They are not randomly generated.它们不是随机生成的。 Those are payment_ids(ask you can see).这些是payment_ids(问你可以看到)。

<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我知道我需要做这样的事情,但目前“14”是硬编码的,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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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