Ask A Question

Notifications

You’re not receiving notifications from this thread.

Nested Parameters returns ActionController::Parameters rather than parameter value

Penguin asked in Rails

Hello,

With the payload:

{
"ref": "The penguin ref",
"object":{
"a": 300,
"b": 30000,
"c": 10022
}
}

params.require(:stuff).permit(:ref, object: [:a, :b, :c]) returns:

{"ref"=>"The penguin ref", "object"=><ActionController::Parameters {"a"=>300, "b"=>30000, "c"=>10022} permitted: true>}

How to receive {"ref"=>"The penguin ref", "object"=> {"a"=>300, "b"=>30000, "c"=>10022}}?

Thanks

Reply

It's the same. Strong params is just a Hash-like object so you don't have to do anything special to it. It's designed like that to work seamlessly but be secure.

Reply

Thanks for the quick answer!
The problem is that I pass params to a custom model that is going to set and validate each attribute. And the validation rejects the value of "object" as it expects something of type Hash but receives a type ActionController::Parameters.

Reply

Ah, well you can always cast it to a hash with to_hash

Reply

Yeah I know. I was hoping there would be a more "built-in" way to achieve that.
Thanks anyway!

Reply

Yep, that's the built-in way. Sounds like your code doing the validation is strict about what type of object it is (a Hash) and that's the part that's not very Ruby-ish.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,464+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.