我最初在rails_api的GitHub上发布了一个问题,但由于没有活动,现在我在这里发布。
我正在尝试在Rails 5 API应用程序中使用rails_admin
。我包含了额外的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 Admin控制台似乎可以正常运行。然而,当我尝试访问应用程序中的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时,才会发生这种情况。
BookingsControllerTest
里面有什么?我认为了解这个会有帮助,因为问题就出在那里... - Uzbekjon