Ale*_*rov 17 json ruby-on-rails activemodel ruby-on-rails-3
如何为所有RoR模型将include_root_in_json设置为false?
我试图在application.rb中设置ActiveModel :: Base.include_root_in_json = false,但它没有任何效果.
ran*_*dra 12
Model.to_json方法用于返回表示模型的JSON字符串,我在模型本身中使用了 self.include_root_in_json = false.
但是对于整个应用程序,您可以尝试在初始化程序中设置ActiveModel :: Base.include_root_in_json = false.[编辑这不起作用.]
[edit] ActiveRecord :: Base.include_root_in_json = true尝试将此行添加到config/environment.rb文件中.
我不确定这对于更高版本的rails是否适用,但对于我(运行2.3.5 atm),在/config/initializers/new_rails_defaults.rb中已经有一个声明用于此设置,其中它被设置为:
ActiveRecord::Base.include_root_in_json = true
Run Code Online (Sandbox Code Playgroud)
因此,如果您尝试在其他位置设置此设置,则可能会覆盖该设置.
| 归档时间: |
|
| 查看次数: |
7137 次 |
| 最近记录: |