gpt4 book ai didi

ruby-on-rails - Rails 3.1 include_root_in_json

转载 作者:行者123 更新时间:2023-12-03 00:48:06 26 4
gpt4 key购买 nike

ActiveRecord::Base.include_root_in_json = true 似乎在 Rails 3.10.rc4 中不起作用,我在文档中没有看到它。

由于根元素现在默认处于关闭状态,我们如何重新启用它?

Rails 3.1 中的

@comments.to_json 现在看起来像

[
{
comment: "Fun street park.",
created_at: 2011-06-29T02:28:29Z,
}
]

在以前的版本中,它有我需要取回的根节点。

[
{
comment: {
comment: "Fun street park.",
created_at: 2011-06-29T02:28:29Z
}
}
]

最佳答案

事实证明,Rails 3.1 只是为您创建了这个 json 配置文件。我不知道这个文件在这里,所以我的初始化程序中的文件被忽略。

在上面 Ryan 的回答中,确实会覆盖此设置。

config/initializers/wrap_parameters.rb

# Be sure to restart your server when you modify this file.
#
# This file contains settings for ActionController::ParamsWrapper which
# is enabled by default.

# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
ActionController::Base.wrap_parameters :format => [:json]

# Disable root element in JSON by default.
if defined?(ActiveRecord)
ActiveRecord::Base.include_root_in_json = false
end

关于ruby-on-rails - Rails 3.1 include_root_in_json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6515436/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com