Rails 3 params unwanted wrapping

前端 未结 1 1665
别那么骄傲
别那么骄傲 2020-12-08 15:51

I\'m posting some JSON like the JSON form of {:name => \"hello\"} to my Rails 3 controller ExampleController.

Instead of getting params

相关标签:
1条回答
  • 2020-12-08 16:15

    ActionController automatically does this for JSON requests so that you can easily pass the parameters into Example.create or @example.update_attributes, which means the client doesn't need to package them up for your model -- it can just include name et. al. at the top level of your JSON data and the controller will handle the grouping.

    @example = Example.create params[:example]
    

    The parameter wrapping code gets the name of your model from the name of the controller, but you can change it using the wrap_parameters macro in your controller:

    wrap_parameters :thing
    

    Or turn it off with

    wrap_parameters false
    

    In Rails 3.2, if your model uses attr_accessible, the parameter wrapping feature will also exclude any parameters that are not accessible to mass assignment. You can also use the macro to make this wrapping feature apply to other content types besides JSON, if you like.

    By default in a newly created Rails app, this is configured for all controllers using an initializer. Look for config/initializers/wrap_parameters.rb.

    0 讨论(0)
提交回复
热议问题