English 中文(简体)
铁路包装参数相对于包括_root_in_json,有什么区别?
原标题:Rails wrap_parameters vs include_root_in_json, what is the difference?

在新的铁路3.2 应用程序中,您可以在配置/初始化器/ wrap_ paraters.rb 中找到下列线条:

ActiveSupport.on_load(:action_controller) do
  wrap_parameters format: [:json]
end

# Disable root element in JSON by default.
ActiveSupport.on_load(:active_record) do
  self.include_root_in_json = false
end

我对第二个代码块的理解是,如果将对象转换为json, 它将不包括根节点( 即用户 @ gt; {: name {gt; John} ), 而是仅仅 {: name { {gt; joh} 。

那么第一个包装参数区块是做什么的? 它对动作- 控制器起作用。 为什么?

最佳回答

包括_ root_ in_json , 用于在铁路中将json 折叠为立体

wrap_paraters 是要包扎从请求中收到的json。

如果您启用了 wrap_paraters , 如果您通过 POST 命令将以下json 发送到铁路 :

名: 约翰·史密斯 {/code>

铁路将自动将 JSON 自动包装为 received :

包括_root_in_json ,另一方面,是对象的json 铁路 < strong > generates 是否通过 to_json 命令包裹。


例如, person.to_json 。 如果启用 包括_root_in_json , 你会得到:

“人”:{姓名:James Brown {/code>

否则,你只会得到

名: 约翰·史密斯 {/code>

问题回答

暂无回答




相关问题
Remove ActiveRecord in Rails 3

Now that Rails 3 beta is out, I thought I d have a look at rewriting an app I have just started work on in Rails 3 beta, both to get a feel for it and get a bit of a head-start. The app uses MongoDB ...

When will you upgrade your app to Rails 3? [closed]

Now that the Rails 3 beta is here, let s take a little straw poll. Please tell us briefly what your application does and when you will upgrade it to Rails 3. Or, if you re not planning on upgrading ...

Bundler isn t loading gems

I have been having a problem with using Bundler and being able to access my gems without having to require them somewhere, as config.gem used to do that for me (as far as I know). In my Rails 3 app, I ...

bypass attr_accessible/protected in rails

I have a model that, when it instantiates an object, also creates another object with the same user id. class Foo > ActiveRecord::Base after_create: create_bar private def create_bar Bar....

concat two fields activerecord

I m so used to oracle where you can simply concat(field1, , field2) but if I m using activerecord to find the field1 and field2, and I need a space in between, how do I accomplish this? Cheers ...