Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails RABL:不带根键的JSON对象_Ruby On Rails_Json_Rabl - Fatal编程技术网

Ruby on rails RABL:不带根键的JSON对象

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' } }, {

我有这个rabl模板:

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”