简体   繁体   中英

Rails strong params with “dot” in name

I need to permit a parameter in Rails 4 which has a dot in it's name:

My params hash looks like the following:

{
  "dictionary_objects.id" => [
    "102", "110", "106"
  ]
}

I can get param value:

>> params['dictionary_objects.id']
=> [
 [0] "102",
 [1] "110",
 [2] "106"
]

But when I try to permit it, it returns an empty hash:

 >> params.permit('dictionary_objects.id')
 Unpermitted parameters: dictionary_objects.id
 => {}

Does anybody know how can I permit params with a dot in it's name?

Thanks.

I think it's just failing to permit it because you've got a collection and you're telling it to permit a single value parameter. If you use:

params.permit(:'dictionary_objects.id' => [])

then all should be well.

for edge cases I recommend a very useful workaround:

params.slice('dictionary_objects.id').permit!

So you do whitelist keys and dont become crazy because of strong params.


sidenote:

rails is builtin to receive args like dictionary_object_ids for has_many relationships, you could leverage this instead.

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