All threads / Nested Parameters returns ActionController::Parameters rather than parameter value

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

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.

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.

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

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

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.

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.