How to set up Rails API app to use ActiveAdmin?

As I said in a previous post, our default development stack uses Ruby on Rails as back-end and ReactJS/React Native as front-end. In almost every project, besides developing the front-end interface for the final user, we need to implement some administration UI for the admins so he could manage/review app data.

When we use Ruby on Rails as back-end server, we usually set the app as API only causing that some functionalities are disabled by default like sessions, cookies, rendering views and so on.

rails new my_api_app --api # Run this command on your terminal to create a rails app in API mode.

Rails ecosystem includes some gems that allows to create admin stuff in an easy way, so most of the time it is easier to use one of these gems to allow admins to manage the data. One of the most used gems int this regard is ActiveAdmin.

Since we had set up our app in API mode, some rails middleware is disabled, the following steps will help us to set up our app so we can use ActiveAdmin without issues:

1. Enable sprockets to allow ActiveAdmin to use assets

To achieve this we need to uncomment the following line in config/application.rb

require "sprockets/railtie"

2. Enable cookies if we are going to use Devise (or any other auth gem)

On the same file config/application.rb we should include the following lines:

config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore

3. Enable MethodOverride

If we want to use all HTTP verbs as DELETE and PATCH we need to add the following line in config/application.rb

config.middleware.use Rack::MethodOverride

This is necessary because these verbs are simulated in Ruby on Rails using a POST request with a param called _method in the request's body.

4. Enable flash messages (notice/alert)

To be able to render flash messages in the pages, we should include the following line also inconfig/application.rb

config.middleware.use ActionDispatch::Flash

5. Modify ApplicationController

ActiveAdmin uses/depends of ApplicationController to works as expected since their controllers inherit from this class. In API mode apps, ApplicationController inherits from ActionController::API and it breaks ActiveAdmin. To fix it we should change application_controller.rb to makes ApplicationController inherits from ActionController::Base

To be able to continue using API stuff, an advise is to create a new controller for example ApiController that inherits from ActionController::API

Finally, our config/application.rb should look something like the following

application.rb

Wrapping up

Although it seems daunting at first, using some simple configuration steps, we could setup a Rails API only app to be able to use ActiveAdmin for the admin management, keeping the advantages of using Rails and React for the rest of the project.

If you like this story clap as many times as you want, and to see similar stories about technology, check our publications and leave us a comment if you have any question.

If you need a team that can help you to implement your ideas in React JS or React Native, feel free to ping us using our website and filling the contact form.

Spanish version

Gustavo
Mail: gustavo@alturasoluciones.com
Twitter: @Aguardientico
LinkedIn: www.linkedin.com/in/gustavoagonzalez
Blog in spanish: http://aguardientech.blogspot.com

Like what you read? Give Gustavo Gonzalez a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.