Rails 向我的 POST 参数添加一个额外的 'command' 对象
Rails adds an extra 'command' object to my POST params
Rails 4.1 正在向我的 POST 参数添加一个奇怪的对象,它是我 post 的数据的副本。
作为一个非常基本的测试,我 post 来自 Angular 的简单 {"msg"=>"hello word!"}
:
$http.post('/commands/save.json', {msg:'hello word!'}).
Rails 得到:
Started POST "/commands/save.json" for ::1 at 2015-01-08 10:15:55 -0800
ActiveRecord::SchemaMigration Load (0.3ms) SELECT `ngconf_schema_migrations`.* FROM `ngconf_schema_migrations`
Processing by CommandsController#save as JSON
Parameters: {"msg"=>"hello word!", "command"=>{"msg"=>"hello word!"}}
{
"msg" => "hello word!",
"controller" => "commands",
"action" => "save",
"format" => "json",
"command" => {
"msg" => "hello word!"
}
}
Commands Load (0.3ms) SELECT `ngconf_commands`.* FROM `ngconf_commands`
Completed 200 OK in 16ms (Views: 1.5ms | ActiveRecord: 12.8ms)
基本上我想了解这是从哪里来的以及如何阻止它。我不记得 Rails 4.0 这样做了。
"command" => {
"msg" => "hello word!"
}
这来自 ParamsWrapper。您可以通过在控制器中指定 wrap_parameters false
来禁用它。如果您想在应用程序范围内禁用它,请编辑您的 config/initializers/wrap_parameters.rb
并根据评论将 :format
选项设置为空数组。举个例子
# Be sure to restart your server when you modify this file.
#
# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
wrap_parameters format: []
end
Rails 4.1 正在向我的 POST 参数添加一个奇怪的对象,它是我 post 的数据的副本。
作为一个非常基本的测试,我 post 来自 Angular 的简单 {"msg"=>"hello word!"}
:
$http.post('/commands/save.json', {msg:'hello word!'}).
Rails 得到:
Started POST "/commands/save.json" for ::1 at 2015-01-08 10:15:55 -0800
ActiveRecord::SchemaMigration Load (0.3ms) SELECT `ngconf_schema_migrations`.* FROM `ngconf_schema_migrations`
Processing by CommandsController#save as JSON
Parameters: {"msg"=>"hello word!", "command"=>{"msg"=>"hello word!"}}
{
"msg" => "hello word!",
"controller" => "commands",
"action" => "save",
"format" => "json",
"command" => {
"msg" => "hello word!"
}
}
Commands Load (0.3ms) SELECT `ngconf_commands`.* FROM `ngconf_commands`
Completed 200 OK in 16ms (Views: 1.5ms | ActiveRecord: 12.8ms)
基本上我想了解这是从哪里来的以及如何阻止它。我不记得 Rails 4.0 这样做了。
"command" => {
"msg" => "hello word!"
}
这来自 ParamsWrapper。您可以通过在控制器中指定 wrap_parameters false
来禁用它。如果您想在应用程序范围内禁用它,请编辑您的 config/initializers/wrap_parameters.rb
并根据评论将 :format
选项设置为空数组。举个例子
# Be sure to restart your server when you modify this file.
#
# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.
# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActiveSupport.on_load(:action_controller) do
wrap_parameters format: []
end