Skip to content

Instantly share code, notes, and snippets.

@raghubetina
Last active April 5, 2020 12:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save raghubetina/02bfdaa2e2811975b409 to your computer and use it in GitHub Desktop.
Save raghubetina/02bfdaa2e2811975b409 to your computer and use it in GitHub Desktop.
ActiveAdmin Cheatsheet

ActiveAdmin Cheatsheet

The ActiveAdmin gem provides us with a entirely separate administrative sub-site. It's amazingly full-featured out of the box, and also is extremely customizable.

Installation

In your Gemfile, include

gem 'activeadmin', '~> 1.0.0.pre4'
gem 'inherited_resources', github: 'activeadmin/inherited_resources'

If you don't already have the Devise gem in your application, you'll need to add that too:

gem 'activeadmin', '~> 1.0.0.pre4'
gem 'inherited_resources', github: 'activeadmin/inherited_resources'
gem 'devise'

and then bundle install.

From the command line:

rails g active_admin:install
rake db:migrate
rake db:seed

Note: If you are using Devise's before_action :authenticate_user! filter in your ApplicationController, then add the following line to config/active_admin.rb:

config.skip_before_filter :authenticate_user!

Restart your rails server.

Usage

Visit http://localhost:3000/admin and log in as the default user:

  • User: admin@example.com
  • Password: password

Voila! You're on your brand new Active Admin dashboard. You should change the email and password of this admin user immediately.

Note: if you get an error relating to ExecJS, try deleting the app/assets/javascripts/active_admin.js.coffee file.

Register your models for administration

To register existing models with Active Admin:

rails generate active_admin:resource neighborhood
rails generate active_admin:resource venue
rails generate active_admin:resource cuisine
rails generate active_admin:resource dish
rails generate active_admin:resource user
rails generate active_admin:resource favorite

Refresh your admin view and you should now have links in your navbar to manage each of these resources.

Whitelist attributes for administration

In the app/admin folder, you will find a file for each model you registered. Within it, uncomment the line that begins with permit_params and customize the list to include any columns that you want to allow to be modified through the admin interface:

# app/admin/venue.rb
permit_params :name, :address, :neighborhood_id

That's it! You now have a fully-functional admin interface that you can use to go in and CRUD data as necessary, without having to complicate your actual controllers and views with a bunch of conditionals. You can now focus entirely on your users there.

Prevent ActiveAdmin CSS from overriding yours

If you find that ActiveAdmin's CSS styles are polluting your own, in app/assets/stylesheets/application.css, delete the line

 *= require_tree .

Further Reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment