Fausto Sanchez Fausto Sanchez - 2 years ago 110
JSON Question

Require only set of nested parameters Rails

I make a request to the server with a Json like this

{"user" : { "name" : "name", "email" : "email", "password":"pswd"}}

So i want to only accept this format Jsons.
I made this:

params.require(:user).permit(:name, :email, :password)

But that doesnt work, i can send more parameters. I want to receive only that format or otherwise, it give an error.


Answer Source

Rails' strong parameters functions as a white-list, meaning that the attributes you specify there will be the only ones that are considered (any extra parameters will be discarded).

If you are worried about extra parameters being processed, resulting in bad data, then what you have will already take care of that.

If you must send an error if there are any extra parameters, you'll need to hand-code that yourself.


extra_params = params.keys - [:name, :email, :password]
if extra_params.size != 0
   # raise an error here in whatever way you wish
   raise "Invalid parameters: #{extra_params}"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download