Setting Up Rails 5 API Only App with ActiveAdmin(updated manual).
*You can also find my outdated post about using ActiveAdmin with Rails 5.0 beta 3.
As you may probably know rails-api gem is now shipped with rails 5 by default. It means we can create beautiful API services using rails without any doubt =)
If you missed the announcement on what’s new in Rails 5, checkout the rails 5.0.0 anounce:**
Generating an API application stripes down Rails a lot, disabling views, flash messages, cookies, and more more, but
ActiveAdmin can still be plugged into it with a bit of tweaking. So here is the steps on how to create a new Rails-5 API application with ActiveAdmin integration:
Setting Up Rails Api
Make sure you have installed any latest stable Ruby version as 2.2.2 or newer is required by Rails 5. I usually use Ruby 2.3.4.
1 2 3 4 5 6
$ rvm list rvm rubies ruby-1.9.3-p551 [ x86_64 ] ruby-2.2.7 [ x86_64 ] => ruby-2.3.4 [ x86_64 ] * ruby-2.4.1 [ x86_64 ]
You also need to have rails 5 installed on your machine:
$ rails --version Rails 5.0.2
if it is not, just install it using
gem install railscommand)
1 2 3 4 5 6 7
$ gem install rails Successfully installed rails-5.0.2 Parsing documentation for rails-5.0.2 Installing ri documentation for rails-5.0.2 Done installing documentation for rails after 0 seconds 1 gem installed
To start building a new Rails API project, we need to generate it by passing the
--apiparameter to the
$ rails new new_api_app --api
Now we have a new tiny-tyne API only Rails application without tons of Front-end related stuff that is useless in case of API apps. The very next step is installing and setting up
RSpec, adding tests, scaffolding resources, etc, etc, but this is out of the scope.
You may probably noted that when an app is created with the
--apiflag, generator did not create views but only api-related resources, e.g.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ cd ./new_api_app $ rails g scaffold user Running via Spring preloader in process 72885 invoke active_record create db/migrate/20170410101135_create_users.rb create app/models/user.rb invoke test_unit create test/models/user_test.rb create test/fixtures/users.yml invoke resource_route route resources :users invoke scaffold_controller create app/controllers/users_controller.rb invoke test_unit create test/controllers/users_controller_test.rb
Adding ActiveAdmin to Rails 5 API app
Even thought Rails 5 API allows us to get rid of senseless Rails’ Front-End parts, we can easily integrate ActiveAdmin into it. We do it by making our
ApplicationController to be inherited from
ActionController::Base instead of
ActionController::API. We will also need to include several classes to the middleware to make it working.
Let’s do it!
Prepare Rails API application.
1 2 3
class ApplicationController < ActionController::API # your code here end
1 2 3
class ApplicationController < ActionController::Base # your code here end
I can also suggest creating a new base controller class for the API that inherits from ActionController::API to separate API and your Admin Panel - Just add a new
ApiController < ActionController::APIclass and use it for all the API-specific logic.
config/application.rbfile to include necessary for ActiveAdmin
ActionDispatch::Cookiesto be able to show flash notifications and use Devise.
1 2 3 4 5 6 7 8 9
module NewApiApp class Application < Rails::Application # ... config.middleware.use ActionDispatch::Flash config.middleware.use Rack::MethodOverride config.middleware.use ActionDispatch::Cookies config.middleware.use ActionDispatch::Session::CookieStore end end
Note: This way your main application code is still API only with all its benefits but you will be able to use ActiveAdmin and it should work as expected.
Add ActiveAdmin and Devise to your
1 2 3 4
source 'https://rubygems.org' # ... gem 'activeadmin', github: 'activeadmin' gem 'devise', '> 4.x'
Update gems using bundler and run the generator.
$ bundle install $ rails g active_admin:install
Update your migrations and start rails to see the results:
1 2 3
$ rails db:migrate $ rails db:seed $ rails server
Visit http://localhost:3000/admin and log in as the default user:
User: email@example.com Password: password
That’s it. =)
Don’t forget to isolate your admin panel from API to protect it from attacks ;)
Keep having a good day!
comments powered by Disqus