Rails strong options with a "dot" in the name

I need to enable a parameter in Rails 4 that has a period in the name:

My params hash is as follows:

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

I can get the parameter value:

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

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

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

Does anyone know how I can resolve params with a dot in the name?

Thanks.

+6
source share
2 answers

I think this simply does not allow this, because you have a collection and you tell it to allow one parameter value. If you use:

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

then everything should be fine.

+6
source

for edge cases I recommend a very useful workaround:

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

So, you do whitelisting and don’t get crazy due to strong parameters.


Sidenote:

rails is built in to get dictionary_object_ids type arguments for has_many , you can use this instead.

+2
source

Source: https://habr.com/ru/post/987068/


All Articles