Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 758
Konstantin Rudy Avatar asked May 12 '15 15:05

Konstantin Rudy


2 Answers

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.

like image 134
Shadwell Avatar answered Sep 28 '22 07:09

Shadwell


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.

like image 42
apneadiving Avatar answered Sep 28 '22 07:09

apneadiving