简体   繁体   English

Rails 6 - 强参数 - 允许数组

[英]Rails 6 - Strong Parameters - allowing array

I am sending this simple hash as JSON to my controller:我将这个简单的哈希作为 JSON 发送到我的控制器:

{
    "cars": [
        { "rego": "ABC123" }
    ]
}

In the controller, I am trying to allow the array of cars for further processing.在控制器中,我试图允许汽车阵列进行进一步处理。

I tried the following:我尝试了以下方法:

params.permit(:cars)
params.permit(cars: [])
params.permit(:cars, cars: [])

In every attempt I am not getting anything in my filtered params:在每次尝试中,我都没有在我的过滤参数中得到任何东西:

DEBUG -- : Unpermitted parameters: :cars, :car, :user_username, :user_token
=> <ActionController::Parameters {} permitted: true>

I am using RoR 6.0.2.1 with Ruby 2.6.5.我正在使用 RoR 6.0.2.1 和 Ruby 2.6.5。

Try params.permit(cars: [:rego])试试params.permit(cars: [:rego])

params.permit(cars: []) allows cars as an array of primitive values params.permit(cars: [])允许汽车作为原始值数组

{
    "cars": [1, 2, 3, 4]
}

" Strong Parameters " has more information. 强参数”有更多信息。

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

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