- Read Tutorial
With our growl notifications properly in place and our admin users configured via single table inheritance, I think it's time we started integrating some custom access points for admin users. Let's start by listing what admin users should be able to do and then we can start creating specs in order to build the features:
- Admin user should be able to create topics
- Non admin users should not be able to create topics
- Admin users should be able to edit topics
- Admin users should be able to see all posts
- Admin users should be able to search for posts
- Admin users should be able to delete posts
And the list goes on and on. Essentially we want admin users to be able to manage the entire site. Our list doesn't even touch on doing things such as banning users or anything like that. We could build our own admin dashboard from scratch, however that would essentially double the time it would take to build the application and much of the code we'd write would need to be updated each time we updated parts of the application. Instead it would make sense to leverage an outside admin library that will do all of this for us and will automatically update as the application grows.
There are a number of popular admin gems, however for this application we're going to Administrate
. In this guide we're going to go through the process of installing the admin dashboard and ensuring that it can only be accessed by admin users.
Installing Administrate Dashboard
We'll start by adding the gem to the Gemfile
. We also need to include the bourbon
gem since it is required for the dashboard.
# Gemfile gem "administrate", "~> 0.2.2" gem 'bourbon'
After running bundle
we can run the generator command in the terminal:
rails generate administrate:install
You may get an error in the terminal such as:
There is a version mismatch between the spring client and the server. You should restart the server and make sure to use the same version. CLIENT: 1.6.2, SERVER: 1.7.2
If so you can follow the instructions and run the following command in the terminal:
spring stop
Now if you run the generator again it should work. As you can see here:
Administrate gives a number of files that will let us manage the application.
Install Bourbon
With the dashboard installed we need to add a call in our master stylesheet file to include the bourbon
styles. Let's just add it to the bottom of the @import
list.
/* app/assets/stylesheets/application.css.scss */ @import "bootstrap-sprockets"; @import "bootstrap"; @import "template/chosen.css"; @import "template/fonts.css"; @import "template/gauge.css"; @import "template/graph.css"; @import "template/jquery.fancySelect.css"; @import "template/main.css"; @import "custom/posts.scss"; @import "template/select2.min.css"; @import "custom/table.css"; @import "custom/topics.scss"; @import "custom/devise_styles.css"; @import "gritter"; @import "bourbon";
Now if you run the server and navigate to localhost:3000/admin
you'll see that our dashboard is up and fully functional.
What's Next
With our dashboard live, next we will implement the security measures needed to ensure that only admin users are able to access the dashboard.