Removing node from RABL

164 views Asked by At

I'm new to rails and I'm trying to create an API for my application. I'm also using rabl for generating my JSON responses. In my rabl template I want to send a timestamp and a collection of items.

The code looks like this:

object false
node(:timestamp){CourseType.maximum("updated_at")}
child(@course_types){attributes :id, :name, :deleted}

And I want to get an object that would look like this:

{"course_types":
    [{"id":2,"name":"Driving","deleted":false},
     {"id":4,"name":"Cooking","deleted":false}],
 "timestamp":"2016-04-25 16:22:57 UTC"}

However I'm getting this instead:

{"course_types":
    [{"course_type":
        {"id":2,"name":"Driving","deleted":false}},
     {"course_type":
        {"id":4,"name":"Cooking","deleted":false}}],
 "timestamp":"2016-04-25 16:22:57 UTC"}

Any ideas on how to solve this?

Thanks

1

There are 1 answers

0
Alex Bezek On BEST ANSWER

Rabl allows you to control these children root nodes across all views via an initializer

https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-mysql-server-part-2

and then setting the correct combination of the configurations

# config/initializers/rabl_init.rb
require 'rabl'
Rabl.configure do |config|
  config.include_json_root = true
  config.include_child_root = false  
end

This might be a duplicate of these questions

Rabl, remove parent element of children

Removing child root nodes in RABL