Set up rabl for Ruby on Rails

fitandfinish edited this page Oct 13, 2011 · 6 revisions

Setting up rabl on a Ruby on Rails project is fairly easy. Infact it's as easy as 1..2..3:

Step 1

Add rabl to your Gemfile:

gem 'rabl'

and run bundle install.

Step 2

Set up your controller to respond to json. You can do this with respond_to or render, whatever you fancy.

class Api::ToursController < ApplicationController
  respond_to :json
  def index 
    @tours = Tour.all
  end
end

Step 3

Now, just add a template with the .rabl extension matching your controller action. For instance, in our example, the template is located at app/views/api/tours/index.json.rabl.

index.json.rabl is where you pull all of this stuff. For instance:

collection @tours

Refer to rabl's doc for more information on usage.