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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With