问题
I'm trying to render a pretty simple data structure using RABL, but I can't figure out how to remove the child root nodes properly. Here are my two templates.
First, the collection index template.
collection @groups, :object_root => false
attributes :id, :name
child :files do
  extends 'groups/_file'
end
And next, the file partial template.
object @file
attributes :id
Those two templates end up producing the following JSON:
[
   {
      "id":"4f57bf67f85544e620000001",
      "name":"Some Group",
      "files":[
         {
            "file":{
               "id":"4f5aa3fef855441009000007"
            }
         }
      ]
   }
]
I want to find a way to remove the root "file" key inside of the files collection. Something like:
[
   {
      "id":"4f57bf67f85544e620000001",
      "name":"Some Group",
      "files":[
         {
            "id":"4f5aa3fef855441009000007"
         }
      ]
   }
]
回答1:
On latest versions of Rabl, you have to set this configuration if you want include_root_json to be false in all levels.
Rabl.configure do |config|
  config.include_json_root = false
  config.include_child_root = false
end
回答2:
Try replacing:
    child :files do
      extends 'groups/_file'
    end
with:
    node :files do |group|
      group.files.map do |file|
        partial 'groups/_file', object: file, root: false
      end
    end
回答3:
This is the usual way of removing the root json (rather than specifying object_root: false)
config/initializers/rabl_config.rb
Rabl.configure do |config|
  config.include_json_root = false
end
Does moving that to there (and restarting rails), fix it?
回答4:
just putting it out there, in case you want to apply it for a specific child:
child :files, :object_root => false do
  extends 'groups/_file'
end
来源:https://stackoverflow.com/questions/9673136/removing-child-root-nodes-in-rabl