加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – RABL API视图目录?

发布时间:2020-12-17 03:15:36 所属栏目:百科 来源:网络整理
导读:我真的很喜欢RABL,但它看起来好像是用.rabl文件来混淆我的视图文件夹.我真的希望理想情况下有一个单独的API视图目录,所以它会是这样的: app/ views/ customers/ index.html.erb show.html.erb ...... api/ v1/ customers/ index.json.rabl show.json.rabl
我真的很喜欢RABL,但它看起来好像是用.rabl文件来混淆我的视图文件夹.我真的希望理想情况下有一个单独的API视图目录,所以它会是这样的:

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

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

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

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

解决方法

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

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

如果你想抛弃这个lineRabl :: Renderer.json(@customers,’api / v1 / customers / index’),只需将配置更改为config.view_paths = [Rails.root.join(‘app’,’views’,’api’,’v1′)].在控制器中它会自动链接它.

希望这可以帮助

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读