0

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

InesM
  • 331
  • 4
  • 16

1 Answers1

1

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

Community
  • 1
  • 1
Alex Bezek
  • 467
  • 2
  • 7