24

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"
         }
      ]
   }
]
BBonifield
  • 4,983
  • 19
  • 36

4 Answers4

48

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
mateusmaso
  • 7,843
  • 6
  • 41
  • 54
  • ive been having this same problem also, and i have tried this solution per the wiki and restarted my server a handful of times, all to no avail. – ohayon Jan 27 '13 at 18:04
12

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
kawty
  • 1,656
  • 15
  • 22
2

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?

Jesse Wolgamott
  • 40,197
  • 4
  • 83
  • 109
0

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
Nuriel
  • 3,731
  • 3
  • 23
  • 23