Ruby on rails RABL:不带根键的JSON对象
我有这个rabl模板:Ruby on rails RABL:不带根键的JSON对象,ruby-on-rails,json,rabl,Ruby On Rails,Json,Rabl,我有这个rabl模板: object @photo attributes :id child :comments do attributes :id, :body end 这给了我这个JSON响应: { photo: { id: 1, comments: [ { comment: { id: 1, body: 'some comment' } }, {
object @photo
attributes :id
child :comments do
attributes :id, :body
end
这给了我这个JSON响应:
{
photo: {
id: 1,
comments: [
{
comment: {
id: 1,
body: 'some comment'
}
},
{
comment: {
id: 2,
body: 'another comment'
}
}
]
}
}
但我希望它看起来像这样:
{
id: 1,
comments: [
{
id: 1,
body: 'some comment'
},
{
id: 2,
body: 'another comment'
}
]
}
为什么rabl用一个名为comment
的额外对象来包装数组中的每个元素。这样,当我使用javascript访问集合时,我必须编写:
var comment = image.comments[0].comment
而不是:
var comment = image.comments[0]
我知道,如果我在@photo
对象的属性列表中包含:comments
,它会按照我想要的方式工作,但是当我想要为每个comment
对象创建另一级别的嵌套关联时,除了使用子对象
之外,没有其他方法来处理它,但这给了我一个我不想要的JSON响应
也许我只是误解了整件事——有人能解释或帮助吗?谢谢 明白了
在config/initializers/rabl_config.rb
中创建一个新文件:
Rabl.configure do |config|
config.include_json_root = false
config.include_child_root = false
end
我遇到了同样的问题,发现除了include\u json\u root=false之外,我还需要添加“config.include\u child\u root=false”