Using rails_admin with rails_api
Asked Answered
B

3

10

I originally posted this as an issue on rails_api GitHub, but am now posting it here due to inactivity.

I'm trying to use rails_admin with a Rails 5 API application. I included extra ActionController modules up to the point that I can either have a functioning rails_admin panel or working API requests. The issue seems to be that rails_admin depends on ActionView::Layouts, which after included causes issues 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 my application to use 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

With these changes the Rails Admin dashboard seems to run fine. However, when I'm trying to access the JSON resources in my application, the following error is thrown:

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"

The 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 I include the ActionView::Layouts module in the top level ActionController::API class to support Rails Admin.

Brioche answered 25/4, 2016 at 12:13 Comment(2)
What is in your BookingsControllerTest? I think it would help to know, since that's where the problem is ...Abracadabra
Have you tried using ActiveModel::Serializers like rails-api recommends?Caddie
A
8

If I were you, I try isolate API and RailsAdmin controllers. I think this must 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 the RailsAdmin::ApplicationController here, and the config settings here.

Abrams answered 28/4, 2016 at 2:0 Comment(0)
B
3

As of v1.0.0 (released September 2016), Rails Admin now supports Rails 5 API-mode straight out-of-the-box. The gem itself injects the missing middleware to render its views and there is no extra configuration required.

Links:

Bromo answered 15/2, 2017 at 15:37 Comment(4)
This is true! Meanwhile I've resorted to updating rails_admin and removing the custom solution mentioned above.Brioche
It worked till 1.1.0 if i'm not wrong; I get ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken): now with Rails 5.1.2 and rails_admin 1.2Alisiaalison
Yes that's correct. There's an open issue with this that I describe hereBromo
What about Devise and it's sign_in view?Oscillator
L
0

You should have a json view file in this location bookings/index.json.jbuilder And inside this file something like

bookings/index.json.jbuilder

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

This is another template missing

application/index

but really don't know you app completely. So maybe that's the application layout you implemented using ActionView::Layouts. In that case is asking you to implement a layout page file in the location application/index.

NOTE: Those two file inside the views folder.

Lowry answered 27/4, 2016 at 19:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.