将 rails_admin 与 rails_api 结合使用 [英] Using rails_admin with rails_api

查看:85
本文介绍了将 rails_admin 与 rails_api 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初将其发布为 rails_api GitHub 上的一个问题,但现在将其发布这里是因为不活动.

I originally posted this as an issue on rails_api GitHub, but am now posting it here due to inactivity.

我正在尝试将 rails_admin 与 Rails 5 API 应用程序一起使用.我包含了额外的 ActionController 模块,直到我可以拥有一个正常运行的 rails_admin 面板或有效的 API 请求.问题似乎是 rails_admin 依赖于 ActionView::Layouts,在包含之后会导致 API 请求出现问题.

I'm trying to use rails_admin with a Rails 5 API application. I included extra ActionController modules up to the point that I can either have a functioning rails_admin panel or working API requests. The issue seems to be that rails_admin depends on ActionView::Layouts, which after included causes issues for API requests.

宝石文件:

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:

I configured my application to use ActionDispatch::Flash:

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

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

I configured extra modules for 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 资源时,会引发以下错误:

With these changes the Rails Admin dashboard seems to run fine. However, when I'm trying to access the JSON resources in my application, the following error is thrown:

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):

The test code (also tried adding 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 之后发生.

This only happens after I include the ActionView::Layouts module in the top level ActionController::API class to support Rails Admin.

推荐答案

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

If I were you, I try isolate API and RailsAdmin controllers. 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

config/initializers/rails_admin.rb

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

只需检查 RailsAdmin::ApplicationController 这里,以及配置设置这里.

Just check the RailsAdmin::ApplicationController here, and the config settings here.

这篇关于将 rails_admin 与 rails_api 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆