Admin Interface
Currently, the only way to create and edit products on our store is through the terminal. Mr. Blackford was OK running the store from the terminal, but he now wants to let a new employee run the store who doesn't have the same tech skills. Can we provide a better admin interface to the store?
Finding a Solution
Currently, our website can only be used to view (or "Read") products, but there isn't a mechanism to Create, Update or Delete products. To fix this, we need to create forms for this purpose. Many website forms are accessed by users, so they need to be customized exactly a specific purpose and they need to be secured so a user can only change what he's supposed to. However, since we just need forms for admins, we can let the admin access all the data on the site without much customization. In fact, it would be good if we could just find a gem that provides an admin with access to everything...
Before reading ahead, can you find search for a gem for this purpose?
To find a gem, go to Ruby-Toolbox.com and search for "admin". This will display the Category for Rails Admin Interfaces.
Looks like there are many gems to choose from, so how do we pick one? Let's consider the two most popular gems more closely, ActiveAdmin and Rails Admin. Scroll down on Ruby-Toolbox and click on the links to each of their Rubygem pages to get more info about the gems.
The RubyGem page lists the different version of the gem and the other gems that this gem depends on. Part of the power of open source is how gems can build off each other, but you need to look at the gems they use to avoid conflicts.
Let's look at ActiveAdmin's dependencies:
Uh-oh! Looks like ActiveAdmin 0.6.6 requires a Rails version less than 4, but our app uses 4.2! We could look into using a more recent non-final version (1.0.0.pre1), but it may make more sense to go with a stable gem that works with Rails 4. rails_admin is compatible with Rails 4 and looks like a good fit for our purposes,1 so let's try it out.
Before starting a major change, make sure you've committed your previous work to git so you have a "checkpoint" to revert to in case you mess up or change your mind.
Adding rails_admin
1. Copy the code from the gem's Rubygem page and add it to your gemfile:
gem 'rails_admin', '~> 0.6.7'
Then run bundle install
on the terminal.
2. According to the gem's readme (on Github), enter rails g rails_admin:install
in the terminal to activate the gem. Hit enter when prompted to keep the default namespace (ie URL).
That's it! Now restart your server and navigate to /admin to view your admin dashboard. You can now browse through all the data on the site and easily create, update and delete records. You'll soon see how to secure this dashboard so only admins can access it.
1. rails_admin will apply to all models by default, which is what we're looking for in this case.