我最初發布爲an issue on rails_api GitHub,但現在由於不活動而在此處發佈。與rails_api一起使用rails_admin
我正在嘗試使用帶有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
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資源在我的應用程序,以下錯誤被拋出:
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
類,以支持Rails的管理員的ActionView::Layouts
模塊。
什麼是你'BookingsControllerTest'?我認爲這將有助於知道,因爲這是問題所在...... – Uzbekjon
@Uzbekjon補充說。 – richard
您是否嘗試過使用[ActiveModel :: Serializers](https://github.com/rails-api/active_model_serializers),如rails-api建議? – trueinViso