Reputation: 12402
I'm working on a Rails 4.1
app with Devise 3.2.4
.
I'm stubbing a mobile API workflow, and I just noticed something weird.
If I submit this POST
to the users controller (here I'm simplifying the URLs):
POST /api/mobile_sign_in.json
body (json):
{
"email" : "[email protected]",
"password" : "foobar"
}
Rails creates this parameters hash:
{ "email" => "[email protected]",
"password" => "foobar",
"user" => {
"email" => "[email protected]"
}
}
That is, it adds a new member user: { email: "string" }
to the parameters.
Something similar happens if I send an empty json object:
POST /api/mobile_sign_in.json
body (json):
{}
which causes Rails to create this parameters hash:
{ "user" => {} }
If a send a truly empty body, the parameters hash is correctly empty.
I'm not using strong parameters for this function (yet).
I can confirm that this happens in other controllers too.
For example, in the LocationsController < ApplicationController
, rails is automatically grouping (and replicating) those attributes that can be mapped to the record.
{ "authentication_token" => "blablablablabla",
"title" => "test title",
"body" => "one two three",
"latitude" => "0.0",
"longitude" => "0.0",
"timestamp" => "2014-04-16T16:28:20.441+01:00",
"user_id" => "1",
"journey_id" => "1",
"location" => {
"title" => "test title",
"body" => "one two three",
"latitude" => "0.0",
"longitude" => "0.0",
"timestamp" => "2014-04-16T16:28:20.441+01:00"
}
}
In this case, the "location" => {}
hash is added automatically.
I'm not yet using strong parameters, and I'm just experimenting with a test tool.
This controller is devise-free.
Upvotes: 0
Views: 186
Reputation: 907
Rails does this automatically for JSON requests to allow for easy access of the model.
It can be turned off by adding the wrap_parameters false
block to a controller.
MyController < ApplicationController
wrap_parameters false
end
For more info, ActionController::ParamsWrapper
Upvotes: 1