Using rails_admin with rails_api

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.

+7
ruby-on-rails ruby-on-rails-5 actionview rails-api rails-admin
source share
3 answers

If I were you, I’ll try to isolate the RailsAdmin APIs and controllers. I think this should work:

 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 

In config/initializers/rails_admin.rb

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

Just check out RailsAdmin::ApplicationController here , and configuration settings here .

+3
source share

As of v1.0.0 (released September 2016), the Rails Admin now supports the Rails 5 API mode out of the box. The stone itself implements the missing middleware to display its representations, and there is no need for additional configuration.

References:

+2
source share

In this directory you should have a json / index.json.jbuilder view file, and inside this file something like

orders / index.json.jbuilder

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

This is another missing template.

Application / Index

but really dont know what you are using. Perhaps this is the layout of the application that you used with ActionView :: Layouts. In this case, you request a page layout file in the application / location index.

NOTE. These two files are in the views folder.

0
source share

All Articles