Skip to content
Daniel Drzimotta edited this page Oct 17, 2016 · 7 revisions

RABL allows for varying levels of flexibility depending on the needs of an API. The most extreme of this flexibility is constructing responses not based on a record at all:

object false # uses no record

node(:message) { "..." }
node(:data) do
  @objects.map { |o| { :name => o.name } }
end
node(:meta) { "..." }

Returns:

{
  message: "...",
  data: [{"name" : "foo" }],
  meta: "..."
}

You can also use RABL partials for much more powerful customization:

object false # uses no record

node(:message) { "..." }
node(:data) do
  partial("rabl/template", :object => @objects)
end
node(:meta) { "..." }

Note that RABL doesn't work with hash data objects. Use OStruct instead.

Also take a look at Using Layouts in RABL as well as other Tips and Tricks.