Cal*_*vin 6 json ruby-on-rails rabl
我有这个rabl模板:
object @photo
attributes :id
child :comments do
attributes :id, :body
end
Run Code Online (Sandbox Code Playgroud)
这给了我这个JSON响应:
{
photo: {
id: 1,
comments: [
{
comment: {
id: 1,
body: 'some comment'
}
},
{
comment: {
id: 2,
body: 'another comment'
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
但我希望它看起来像这样:
{
id: 1,
comments: [
{
id: 1,
body: 'some comment'
},
{
id: 2,
body: 'another comment'
}
]
}
Run Code Online (Sandbox Code Playgroud)
为什么rabl用一个额外的对象来包装数组中的每个元素comment.这样当我在javascript中访问集合时,我必须写:
var comment = image.comments[0].comment
Run Code Online (Sandbox Code Playgroud)
代替:
var comment = image.comments[0]
Run Code Online (Sandbox Code Playgroud)
我知道如果我:comments在@photo对象的属性列表中包含它按照我想要的方式工作,但是当我想为每个comment对象建立另一级别的嵌套关联时,除了使用之外没有办法处理它child,但这给了我我不想要的JSON响应.
也许我只是误解了整个事情 - 有人可以解释或帮助吗?谢谢!
得到它了!
在以下位置创建新文件config/initializers/rabl_config.rb:
Rabl.configure do |config|
config.include_json_root = false
config.include_child_root = false
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1357 次 |
| 最近记录: |