Search code examples
ruby-on-railsrubyrabl

Removing child root nodes in RABL


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"
         }
      ]
   }
]

Solution

  • 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