Setting Up Active Admin With Rails 6 API

Jonathan Zavala
4 min readMar 4, 2022

When creating a E-commerce website it’s usually a good idea to create an admin user that can preform CRUD actions on the products or users that sign up for the application. I decided to use ActiveAdmin with my rails API, but this is where we run into problems. ActiveAdmin is normally used with a regular rails application not just the API. The difference is with the api, the views and some middleware and not included and ActiveAdmin depends on those things. So how do we work around these issues?

Step 1

After creating your rails API you’ll need to switch out your ActionController::API in your application_controller.rb file to ActionController::Base.

#before
class ApplicationController < ActionController::API
end
#after
class ApplicationController < ActionController::Base
end

Unfortunately now any controller we make won’t have the benefits of the API. So in order to have action to those benefits we have to make another controller something like api_controller.rb and inside of that controller you’ll want to inherit your ActionController::API. From now on any controller you create will inherit the ApiController.

class ApiController < ActionController::API
end
# creating new controllerclass NewController < ApiController
end

Step 2

Now we’ll add back in that middleware that we threw out when creating the API. You’ll want to add these to you config/application.rb file of your application I usually put mine under the config.api_only = true line. These are the middleware you’ll need to add back in:

# config/application.rbconfig.middleware.use Rack::MethodOverride
config.middleware.use ActionDispatch::Flash
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore

In that same file you’ll need to uncomment a specific requirement called sprockets/railtie. After you comment that in your application.rb file should look like this:

# config/application.rbrequire_relative "boot"require "rails"# Pick the frameworks you want:require "active_model/railtie"require "active_job/railtie"require "active_record/railtie"require "active_storage/engine"require "action_controller/railtie"require "action_mailer/railtie"require "action_mailbox/engine"require "action_text/engine"require "action_view/railtie"require "action_cable/engine"require "sprockets/railtie"# require "rails/test_unit/railtie"# Require the gems listed in Gemfile, including any gems# you've limited to :test, :development, or :production.Bundler.require(*Rails.groups)module FightArmor class Application < Rails::Application  # Initialize configuration defaults for originally generated Rails version.  config.load_defaults 6.1  # Configuration for the application, engines, and railties goes here.  # These settings can be overridden in specific environments using the files  # in config/environments, which are processed later.  # config.time_zone = "Central Time (US & Canada)"  # config.eager_load_paths << Rails.root.join("extras")  # Only loads a smaller set of middleware suitable for API only apps.  # Middleware like session, flash, cookies can be added back  manually.  # Skip views, helpers and assets when generating a new resource.  config.api_only = true  config.middleware.use Rack::MethodOverride  config.middleware.use ActionDispatch::Flash  config.middleware.use ActionDispatch::Cookies  config.middleware.use ActionDispatch::Session::CookieStore endend

Step 3

After you’ve made sure that your database is created by running rails:db create, you’ll want to add a few things to your Gemfile.

# Gemfile
gem 'devise'
gem 'activeadmin'
gem 'sprockets', '<4'
gem 'sass-rails'

The reason you want sprockets to be lower than the 4th version is because it does play well rails 6 as an api. It’ll throw an error in your console that looks like this:

if Bootsnap::Load Path Cache::FALLBACK SCAN.equal?(resolved) if (cursor = Bootsnap::Load PathCache.loaded_features_index.cursor(string_path)) ret = require_without_bootsnap(path) resolved = Bootsnap::LoadPathCache.loaded_features_index.identify(string_path, cursor) Bootsnap::LoadPathCache.loaded_features_index.register(string_path, resolved) return ret...

No fun after you correct that you’ll also get an error if you don’t add gem ‘sass-rails’ that looks like this in your browser:

LoadError in ActiveAdmin::Devise::SessionsController#new cannot load such file -- sass

So make sure you have those two gems in there before you run bundle install! Now run bundle install and rails g active_admin:install in your console. This should create almost every thing you need for ActiveAdmin. Run rails s and go to localhost:3000/admin and you should come to a log in page.

Step 4

You’re looking at this log in screen like “hmm I never made a log in” you’re correct. We have to make a user in our rails console.

#rails consoleuser = AdminUser.new
#press enter
user.email = whatever@emailyouwant.com
#press enter
user.password = whateverpasswordyouwant
#press enter
user.password_confirmation = same password as above
#press enter
user.save!
#press enter

This will create the admin user you need to sign in. Run rails s and refresh the localhost:3000/admin and log in with the user you created and just like that in your in!

Final Step

Now you’ll need add production to your application by running rails g scaffold products name:string price:integer. This will not create the controller so you have to make it on your own because you will have to inherit the ApiController like so:

class ProductsController < ApiController
end

Finally you’ll be able to add products to the admin options by running the following command in your rails console:

rails generate active_admin:resource Products

Now you can preform CRUD action on the products of your E-commerce application as an admin and regular users can’t preform any unwanted actions.

end

--

--