7

我最初發布爲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模塊。

+0

什麼是你'BookingsControllerTest'?我認爲這將有助於知道,因爲這是問題所在...... – Uzbekjon

+0

@Uzbekjon補充說。 – richard

+0

您是否嘗試過使用[ActiveModel :: Serializers](https://github.com/rails-api/active_model_serializers),如rails-api建議? – trueinViso

回答

3

如果我是你,我嘗試隔離API和RailsAdmin控制器。我想,這一定工作:

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::ApplicationControllerhere和配置設置here

0

你應該在這個位置預訂一個JSON視圖文件/ index.json.jbuilder 而這個文件類似

預訂內部/ index.json.jbuilder

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

這是另一個模板缺少

應用/索引

但真的完全不知道你的應用程序。所以也許這就是你使用ActionView :: Layouts實現的應用佈局。在這種情況下,要求您在位置應用程序/索引中實現佈局頁面文件。

注意:視圖文件夾內的那兩個文件。

2

v1.0.0(2016年9月發佈)開始,Rails Admin現在支持直接使用Rails 5 API模式。寶石本身注入缺失的中間件來渲染其視圖,並且不需要額外的配置。

鏈接:

+0

這是真的!同時,我採取了更新rails_admin並刪除上述自定義解決方案。 – richard

+0

如果我沒有錯,它會工作到'1.1.0';我得到了'ActionController :: InvalidAuthenticityToken(ActionController :: InvalidAuthenticityToken):'現在使用Rails 5.1.2和rails_admin 1.2 –

+0

是的,這是正確的。有一個公開的問題,我描述[這裏](http://www.carlosramireziii.com/how-to-use-rails-admin-with-a-rails-5-api-application.html#comment-3399871556 ) –