5

I really like RABL, but it's seeming like it's going to clutter up my views folders with .rabl files. I really want to ideally have a seperate API views directory so it would be like this:

app/
    views/
        customers/
            index.html.erb
            show.html.erb
            ......
        api/
            v1/
                customers/
                    index.json.rabl
                    show.json.rabl

What's the best way of achieving this? I'm using this tutorial:

http://railscasts.com/episodes/350-rest-api-versioning

To set up versioning but it doesn't support RABL. I have tried this in app/controllers/api/v1/customers_controller.rb:

module Api
    module V1
        class CustomersController < ApplicationController
            respond_to :json

            def index
                @customers = Customer.search(params[:page])

                Rabl::Renderer.json(@customers, 'api/v1/customers/index')
            end
        end
    end
end

But as expected that didn't seem to work.

andy
  • 2,369
  • 2
  • 31
  • 50

2 Answers2

5

Had the same problem. And solved it by adding this in the RABL initializer

# config/initializers/rabl_init.rb
require 'rabl'
Rabl.configure do |config|
    config.view_paths = [Rails.root.join('app', 'views')]
end

If you want to ditch this lineRabl::Renderer.json(@customers, 'api/v1/customers/index') just change to configuration to be config.view_paths = [Rails.root.join('app', 'views', 'api', 'v1')]. In the controller it will automatically link it.

Hope this helps

Seif Sallam
  • 821
  • 2
  • 10
  • 30
0

From what I can see this should work. I'm doing pretty much the same thing. What errors are you getting?

Rick Roberts
  • 885
  • 11
  • 13