简体   繁体   English

将rails_admin与rails_api一起使用

[英]Using rails_admin with rails_api

I originally posted this as an issue on rails_api GitHub , but am now posting it here due to inactivity. 我最初在rails_api GitHub上发布了这个问题 ,但现在由于不活动而在此发布。

I'm trying to use rails_admin with a Rails 5 API application. 我正在尝试将rails_admin与Rails 5 API应用程序一起使用。 I included extra ActionController modules up to the point that I can either have a functioning rails_admin panel or working API requests. 我添加了额外的ActionController模块,以至于我可以使用有效的rails_admin面板或工作API请求。 The issue seems to be that rails_admin depends on ActionView::Layouts , which after included causes issues for API requests. 问题似乎是rails_admin依赖于ActionView::Layouts ,后面包含会导致API请求出现问题。

Gemfile: 的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'

I configured my application to use ActionDispatch::Flash : 我将我的应用程序配置为使用ActionDispatch::Flash

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

I configured extra modules for Rails API , ApplicationController: 我为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

With these changes the Rails Admin dashboard seems to run fine. 通过这些更改,Rails管理仪表板似乎运行正常。 However, when I'm trying to access the JSON resources in my application, the following error is thrown: 但是,当我尝试访问应用程序中的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"

The test code (also tried adding format: :json ): 测试代码(也试过添加format: :json ):

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

    assert_response :success
  end
end

This is the controller code: 这是控制器代码:

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

This only happens after I include the ActionView::Layouts module in the top level ActionController::API class to support Rails Admin. 这只发生在我在顶级ActionController::API类中包含ActionView::Layouts模块以支持Rails Admin之后。

If I were you, I try isolate API and RailsAdmin controllers. 如果我是你,我会尝试隔离API和RailsAdmin控制器。 I think this must work: 我认为这必须奏效:

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

In config/initializers/rails_admin.rb config/initializers/rails_admin.rb

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

Just check the RailsAdmin::ApplicationController here , and the config settings here . 只需在此处检查RailsAdmin::ApplicationController ,并在此处查看配置设置。

As of v1.0.0 (released September 2016), Rails Admin now supports Rails 5 API-mode straight out-of-the-box. v1.0.0 (2016年9月发布)开始,Rails Admin现在支持Rails 5 API模式直接开箱即用。 The gem itself injects the missing middleware to render its views and there is no extra configuration required. gem本身会注入缺少的中间件来呈现其视图,并且不需要额外的配置。

Links: 链接:

You should have a json view file in this location bookings/index.json.jbuilder And inside this file something like 你应该在这个位置预订/ index.json.jbuilder中有一个json视图文件。在这个文件里面有类似的东西

bookings/index.json.jbuilder 预订/ index.json.jbuilder

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

This is another template missing 这是另一个缺少的模板

application/index 应用/索引

but really don't know you app completely. 但真的不完全了解你的应用程序。 So maybe that's the application layout you implemented using ActionView::Layouts. 也许这就是你使用ActionView :: Layouts实现的应用程序布局。 In that case is asking you to implement a layout page file in the location application/index. 在这种情况下,要求您在位置应用程序/索引中实现布局页面文件。

NOTE: Those two file inside the views folder. 注意:视图文件夹中的那两个文件。

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

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