I originally posted this as a problem on rails_api GitHub , but now I am posting it here due to inaction.
I am trying to use rails_admin using a Rails 5 API application. I have included additional ActionController modules up to the point that I have either a working rails_admin panel or working API requests. The problem is that rails_admin is dependent on ActionView::Layouts , which after turning on causes problems for API requests.
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'
I configured the application to use ActionDispatch::Flash :
module MyApp class Application < Rails::Application ... config.middleware.use ActionDispatch::Flash end end
I configured additional modules for the 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
With these changes, the Rails Admin dashboard works fine. However, when I try to access JSON resources in my application, the following error occurs:
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"
Test code (also tried adding format: :json ):
class BookingsControllerTest < ActionController::TestCase test 'should get index' do get :index assert_response :success end end
This is the controller code:
class BookingsController < ApplicationController def index @bookings = find_bookings render json: @bookings, include: ['customer', 'client'], meta: meta end end
This only happens after the ActionView::Layouts module is ActionView::Layouts in the top-level class of the ActionController::API to support Rails Admin.