【发布时间】:2016-08-18 19:47:48
【问题描述】:
我最初以an issue on rails_api GitHub 发布此内容,但由于不活跃,现在将其发布在此处。
我正在尝试将rails_admin 与 Rails 5 API 应用程序一起使用。我包含了额外的 ActionController 模块,直到我可以拥有一个正常工作的 rails_admin 面板或工作 API 请求。问题似乎是 rails_admin 依赖于ActionView::Layouts,在包含之后会导致 API 请求出现问题。
宝石文件:
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
I configured extra modules for Rails API,应用控制器:
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
只有在我将 ActionView::Layouts 模块包含在顶级 ActionController::API 类中以支持 Rails Admin 后才会发生这种情况。
【问题讨论】:
-
您的
BookingsControllerTest中有什么内容?我认为这会有所帮助,因为这就是问题所在...... -
您是否尝试过像 rails-api 推荐的那样使用ActiveModel::Serializers?
标签: ruby-on-rails rails-admin actionview rails-api ruby-on-rails-5