Setting Up Rails 5 API Only App with ActiveAdmin enabled
Note: This article is out of date since Rails 5 has been already released. Here is an updated version of this article.
A new version of Rails 5.0 beta 3 was published several days ago while the final version is to be released on March 16. It means we can start playing with it right now! Yay!!
As you may probably know rails-api gem is now shipped with rails 5 by default. It means that 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 Ruby 2.2.2 or newer as it is required by Rails 5. I use Ruby 2.3.0 as it was recently released as well and I want to try it out.
1 2 3 4 5 6 7 8 9 10
$ rvm list rvm rubies ruby-1.9.3-p551 [ x86_64 ] ruby-2.1.7 [ x86_64 ] * ruby-2.2.4 [ x86_64 ] => ruby-2.3.0 [ x86_64 ] # => - current # =* - current && default # * - default
Rails 5 hasn’t been officially released yet so we have to clone Rails from its GitHub repository(
$ git clone email@example.com:rails/rails.git
Go into the newly cloned rails dir, and run the
$ bundle install
To start building a new Rails API project, we need to generate it by passing the
--apiparameter to the
$ bundle exec railties/exe/rails new ../new_api_app --api --edge
I am running this command from the directory where cloned version of rails is located. It means I have to put an additional param with the directory name started from
../to put the newly generated application into a place above current wd.
../new_api_appin this case.
The next step is to cd into directory of your new rails API application and run
rake db:setupto install all the necessary gems and setup the database:
1 2 3
$ cd ../new_api_app $ bundle install $ bin/rake db:setup
Note that I am using
bin/rake(and will be using
bin/railsin the next steps) cause this version of rails was not installed globally and the full path to appropriate binary should be used.
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 would be: installing and setting up
RSpec, adding tests, scaffolding resources, etc, etc, but this part is out of my theme.
You have probably noted that when an app is created with the
--apiflag, generator did not create views but only api-related resources were created, e.g.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
$ bin/rails g scaffold user Running via Spring preloader in process 55287 invoke active_record create db/migrate/20160228093108_create_users.rb create app/models/user.rb invoke rspec create spec/models/user_spec.rb invoke factory_girl create spec/factories/users.rb invoke resource_route route resources :users invoke serializer create app/serializers/user_serializer.rb invoke scaffold_controller create app/controllers/users_controller.rb invoke rspec create spec/controllers/users_controller_spec.rb create spec/routing/users_routing_spec.rb invoke rspec create spec/requests/users_spec.rb
Adding ActiveAdmin to Rails 5 API app
Even thought Rails 5 API allows us to get rid of senseless Rails’ Front-End parts, generating a new project using
--api param, we can easily integrate ActiveAdmin into it.
They did it by having base API controller inherit from
ActionController::API instead of
ApplicationController so integrating ActiveAdmin is as easy as setting
ApplicationController to be inherited from
We 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
config/application.rbfile to include necessary for ActiveAdmin
1 2 3 4 5 6 7 8
module NewApiApp class Application < Rails::Application # ... config.middleware.use ActionDispatch::Flash config.middleware.use Rack::MethodOverride config.middleware.use ActionDispatch::Cookies 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 to your
1 2 3
source 'https://rubygems.org' # ... gem 'activeadmin', github: 'activeadmin'
ActiveAdmin master has preliminary support for Rails 5 and to give it a try, these Gems should be installed:
1 2 3 4 5 6 7 8 9 10 11 12 13
gem 'devise', '> 4.x' gem 'rspec-rails', '>= 3.5.0.beta1' gem 'ransack', github: 'activerecord-hackery/ransack' gem 'kaminari', github: 'amatsuda/kaminari', branch: '0-17-stable' gem 'formtastic', github: 'justinfrench/formtastic' gem 'draper', github: 'audionerd/draper', branch: 'rails5', ref: 'e816e0e587' # To fix a Draper deprecation error gem 'activemodel-serializers-xml', github: 'rails/activemodel-serializers-xml' # Optional -- only if you already include these gems gem 'rack-mini-profiler', github: 'MiniProfiler/rack-mini-profiler' gem 'database_cleaner', github: 'pschambacher/database_cleaner', branch: 'rails5.0', ref: '8dd9fa4'
Update gems using bundler:
$ bundle install
After installing the gems, you need to run the generator.
$ bin/rails g active_admin:install
Here are another options, like setting it to use an existing user class: activeadmin/docs/0-installation.md
Update your migrations and start rails to the results:
1 2 3
$ rake db:migrate $ rake db:seed $ rails server
Visit http://localhost:3000/admin and log in as the default user:
User: firstname.lastname@example.org Password: password
That’s it. =)
Don’t forget to isolate your admin page to protect it from attacks. You can, for example, filter out access to it so it is available from your internal IP address only.
Keep having a good day!
comments powered by Disqus