Rails增加了一个额外的命令'反对我的POST参数

时间:2015-01-08 18:56:24

标签: ruby-on-rails ruby

Rails 4.1正在为我的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!"
        }

1 个答案:

答案 0 :(得分:0)

这来自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