RABL API视图目录?

and*_*ndy 5 api ruby-on-rails rabl

我真的很喜欢RABL,但它似乎会让我的视图文件夹混乱.rabl.我真的希望理想情况下有一个单独的API视图目录,所以它会是这样的:

app/
    views/
        customers/
            index.html.erb
            show.html.erb
            ......
        api/
            v1/
                customers/
                    index.json.rabl
                    show.json.rabl
Run Code Online (Sandbox Code Playgroud)

实现这一目标的最佳方法是什么?我正在使用这个教程:

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

要设置版本控制但不支持RABL.我试过这个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
Run Code Online (Sandbox Code Playgroud)

但正如预期的那样似乎没有用.

Sei*_*lam 5

有同样的问题.并通过在RABL初始化程序中添加它来解决它

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

如果你想抛弃这条线,Rabl::Renderer.json(@customers, 'api/v1/customers/index')只需改为配置即可config.view_paths = [Rails.root.join('app', 'views', 'api', 'v1')].在控制器中它会自动链接它.

希望这可以帮助

  • 这如何处理版本控制? (5认同)