Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RABL API Views Directory?

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.

like image 423
andy Avatar asked Jan 16 '13 09:01

andy


1 Answers

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

like image 154
Seif Sallam Avatar answered Oct 25 '22 08:10

Seif Sallam