Search code examples
ruby-on-railsrubyrabl

add data to root hash with RABL


I'd like to add request info to response from RABL

my code looks like

object @collection => :response
attributes :id, :submitted_at, :address, :name, :data
rest_of_code_omitted

I've tried to add

node(:request, :object_root =>true ) do
   {request: {
       url: request.original_url,
       status: 200,
       errors: {
       } }}
 end

but this code adds request node to each object from collection, while i want this node to be rendered once above collection node. Is there any way to achieve that?


Solution

  • Here you go.

    object false
    node :request do
      {
        request: {
          url: request.original_url,
          status: 200,
          errors: { }
        }
      }
    end
    
    child(@collection => :response) do
      attributes :id, :submitted_at, :address, :name, :data
    end
    

    This should do the trick. object false directive is what puts the node at the root level.