如何将所有 RoR 模型的 include_root_in_json 设置为 false?

发布于 2024-10-06 20:33:07 字数 141 浏览 2 评论 0原文

如何将所有 RoR 模型的 include_root_in_json 设置为 false?

我尝试在 application.rb 中设置 ActiveModel::Base.include_root_in_json = false ,但没​​有效果。

How can i set include_root_in_json to false for all my RoR models?

I've tried to set ActiveModel::Base.include_root_in_json = false inside application.rb, but it has no effect.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寄居人 2024-10-13 20:33:07

Model.to_json 方法用于返回代表模型的 JSON 字符串,我已经使用了
self.include_root_in_json = false 在模型本身中。

但对于整个应用程序,您可以尝试在初始值设定项中设置 ActiveModel::Base.include_root_in_json = false。[编辑这不起作用。]

[编辑] ActiveRecord::Base.include_root_in_json = true 尝试将此行添加到 config/environment.rb 文件中。

Model.to_json method is used to returns a JSON string representing the model and i have used
self.include_root_in_json = false in a model itself.

But for the whole application you can try setting ActiveModel::Base.include_root_in_json = false in an initializer.[edit This does not work.]

[edit] ActiveRecord::Base.include_root_in_json = true try adding this line to config/environment.rb file.

烟若柳尘 2024-10-13 20:33:07

我不确定这对于 Rails 的更高版本是否成立,但对于我(运行 2.3.5 atm)来说, /config/initializers/new_rails_defaults.rb 中已经有此设置的声明,其中它被设置为:

ActiveRecord::Base.include_root_in_json = true

所以如果如果您尝试在其他地方设置此设置,那么它可能会被覆盖。

I'm not sure if this is true for the later versions of rails but for me (running 2.3.5 atm) there was already a declaration for this setting in /config/initializers/new_rails_defaults.rb where it was set to:

ActiveRecord::Base.include_root_in_json = true

So if you try to set this setting in another place then it might be overridden.

冰葑 2024-10-13 20:33:07

在 Rails 3.2.2 上,这已在初始化程序中设置:

---config/initializers/wrap_parameters.rb ---

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

On Rails 3.2.2, this is already set in the initializers:

---config/initializers/wrap_parameters.rb ---

# Disable root element in JSON by default.
ActiveSupport.on_load(:active_record) do
  self.include_root_in_json = false
end
在巴黎塔顶看东京樱花 2024-10-13 20:33:07

对于默认的系统范围配置,在初始化程序中设置变量始终是个好主意,例如 config/initializers/defaults.rb

For default system wide config is alway good idea to set variable in initializers, like config/initializers/defaults.rb

乄_柒ぐ汐 2024-10-13 20:33:07

我认为你应该在 config/application.rb 中设置 ActiveRecord::Base.include_root_in_json = false

而不是 ActiveModel::Base

I think you should set ActiveRecord::Base.include_root_in_json = false in config/application.rb

not ActiveModel::Base

你与昨日 2024-10-13 20:33:07

在 Rails 4 的“config/initializers/wrap_elements.rb”中,您可以取消注释以下内容:

ActiveSupport.on_load(:active_record) do
 self.include_root_in_json = true
end 

At Rails 4 in "config/initializers/wrap_elements.rb" you can uncomment the following:

ActiveSupport.on_load(:active_record) do
 self.include_root_in_json = true
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文