7

私はもともとan issue on rails_api GitHubとして掲示しましたが、現在は非アクティブのために投稿しています。rails_adminをrails_apiで使用する

rails_adminをRails 5 APIアプリケーションで使用しようとしています。私は、機能するrails_adminパネルまたは動作中のAPIリクエストを持つことができるまで、余分なActionControllerモジュールを追加しました。問題は、rails_adminがActionView::Layoutsに依存していることが原因であると見なされます。これが含まれると、APIリクエストの問題が発生します。

Gemfile:Railsの管理ダッシュボードが正常に動作すると思われるこれらの変更により

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 

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

I configured extra modules for Rails API、ApplicationControllerに:

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を使用するために自分のアプリケーションを構成し。しかし、私は自分のアプリケーションで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 

これはコントローラであり、コード:私はRailsの管理をサポートするために、トップレベルActionController::APIクラスでActionView::Layoutsモジュールが含まれた後

class BookingsController < ApplicationController 
    def index 
    @bookings = find_bookings 
    render json: @bookings, include: ['customer', 'client'], meta: meta 
    end 
end 

にのみ起こります。

+0

あなた 'BookingsControllerTest'には?私は、問題がどこにあるのか知っておくと助けになると思います... – Uzbekjon

+0

@Uzbekjonが追加されました。 – richard

+0

rails-apiが推奨する[ActiveModel :: Serializers](https://github.com/rails-api/active_model_serializers)を試したことがありますか? – trueinViso

答えて

3

私があなただったら、APIとRailsAdminコントローラを分離しようとします。ただ、RailsAdmin::ApplicationControllerhereをチェック

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 

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

config/initializers/rails_admin.rbでは、とコンフィグ設定here:私はこれが働かなければならないと思います。

0

あなたは、これは別のテンプレートで何か

などの予約/ index.json.jbuilder

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

予約/ index.json.jbuilder、このファイル内のこの場所にJSONビューファイルを持っている必要があります行方不明

アプリケーション/インデックス

本当にあなたは完全にアプリを知っていません。 ActionView :: Layoutsを使って実装したアプリケーションのレイアウトです。その場合は、アプリケーション/インデックスの場所にレイアウトページファイルを実装するように求められます。

注:ビューフォルダ内の2つのファイルです。

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)について記述しています。 ) –

関連する問題