繁体   English   中英

将rails_admin与rails_api一起使用

[英]Using rails_admin with rails_api

我最初在rails_api GitHub上发布了这个问题 ,但现在由于不活动而在此发布。

我正在尝试将rails_admin与Rails 5 API应用程序一起使用。 我添加了额外的ActionController模块,以至于我可以使用有效的rails_admin面板或工作API请求。 问题似乎是rails_admin依赖于ActionView::Layouts ,后面包含会导致API请求出现问题。

的Gemfile:

gem 'rails', '>= 5.0.0.beta3', '< 5.1'
...
gem 'rack-pjax', github: 'afcapel/rack-pjax'
gem 'remotipart', github: 'mshibuya/remotipart'
gem 'kaminari', github: 'amatsuda/kaminari', branch: '0-17-stable'
gem 'rails_admin', github: 'sferik/rails_admin'

我将我的应用程序配置为使用ActionDispatch::Flash

module MyApp
  class Application < Rails::Application
    ...
    config.middleware.use ActionDispatch::Flash
  end
end

我为Rails API ,ApplicationController 配置了额外的模块

class ApplicationController < ActionController::API
  include Knock::Authenticatable
  include Pundit

  # RailsAdmin support
  include AbstractController::Helpers
  include ActionController::Flash
  include ActionController::RequestForgeryProtection
  include ActionController::MimeResponds
  include ActionController::HttpAuthentication::Basic::ControllerMethods
  include ActionView::Layouts
end

通过这些更改,Rails管理仪表板似乎运行正常。 但是,当我尝试访问应用程序中的JSON资源时,会引发以下错误:

Error:
BookingsControllerTest#test_should_get_index:
ActionView::MissingTemplate: Missing template bookings/index, application/index with {:locale=>[:en], :formats=>[:json], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :haml]}. Searched in:
  * "/Users/richard/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/bundler/gems/rails_admin-355dc80f8a20/app/views"

测试代码(也试过添加format: :json ):

class BookingsControllerTest < ActionController::TestCase
  test 'should get index' do
    get :index

    assert_response :success
  end
end

这是控制器代码:

class BookingsController < ApplicationController
  def index
    @bookings = find_bookings
    render json: @bookings, include: ['customer', 'client'], meta: meta
  end
end

这只发生在我在顶级ActionController::API类中包含ActionView::Layouts模块以支持Rails Admin之后。

如果我是你,我会尝试隔离API和RailsAdmin控制器。 我认为这必须奏效:

class ApplicationController < ActionController::API
  include Knock::Authenticatable
  include Pundit
end

class RailsAdminCustomController < ApplicationController
  # RailsAdmin support
  include AbstractController::Helpers
  include ActionController::Flash
  include ActionController::RequestForgeryProtection
  include ActionController::MimeResponds
  include ActionController::HttpAuthentication::Basic::ControllerMethods
  include ActionView::Layouts
end

config/initializers/rails_admin.rb

RailsAdmin.config do |config|
  # other config stuff ...
  config.parent_controller = '::RailsAdminCustomController'
end

只需在此处检查RailsAdmin::ApplicationController ,并在此处查看配置设置。

v1.0.0 (2016年9月发布)开始,Rails Admin现在支持Rails 5 API模式直接开箱即用。 gem本身会注入缺少的中间件来呈现其视图,并且不需要额外的配置。

链接:

你应该在这个位置预订/ index.json.jbuilder中有一个json视图文件。在这个文件里面有类似的东西

预订/ index.json.jbuilder

json.name @bookings.name
json.date @bookings.date

这是另一个缺少的模板

应用/索引

但真的不完全了解你的应用程序。 也许这就是你使用ActionView :: Layouts实现的应用程序布局。 在这种情况下,要求您在位置应用程序/索引中实现布局页面文件。

注意:视图文件夹中的那两个文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM