Rails wrap_parameters vs include_root_in_json, what is the difference?

前端 未结 1 738
甜味超标
甜味超标 2020-12-13 19:48

In a new Rails 3.2 app you can find in config/initializers/wrap_parameters.rb the following lines:

ActiveSupport.on_load(:action_controller) do
  wrap_parame         


        
相关标签:
1条回答
  • 2020-12-13 20:28

    include_root_in_json is to wrap json instantiated in Rails

    wrap_parameters is to wrap json received from a request.

    If you have wrap_parameters enabled, and if you send the following json through a POST command to Rails:

    {name: 'John Smith'}

    Rails will automatically wrap the JSON it received into:

    {"person": {name: 'John Smith'}}

    include_root_in_json, on the other hand, is whether the json Rails generates from an object is wrapped or not through the to_json command.


    e.g. Person.to_json. If include_root_in_json is enabled, you'll get:

    {"person": {name: 'James Brown'}}

    Otherwise, you'll just get

    {name: 'John Smith'}

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