I have thought a while about making a summary post about configuring devise + mongoid + cancan for creating accounts and implementing authentication in a rails app.
First of all there are various guides that you can use to get you started. All of them can give you a different insight on the technology and a different perspective and what is needed to accomplish whatever you have in mind :).
So feel free to google for devise + cancan and you end up reading Tony Amoyal’s blog post. It is a very good post description and pointed me toward the right direction.
If you want a different point of view though (also considering that I am using mongoid), here is my dirty summary on how to configure an app that would use these gems:
First of all install devise:
Devise 2.0 works with Rails 3.1 onwards. You can add it to your Gemfile with: gem ‘devise’ or run the bundle command.
After devise has been installed, run the generator:
rails generate devise:install
The generator installs an initializer describing All devise’s configuration options and it is advised to have a look at it. Then add devise to your models using the generator:
rails generate devise MODEL
More details are available here: https://github.com/plataformatec/devise; together with links to some example app that could be extended to suit your needs.
Now let’s install cancan. In Rails 2 you can add it to your Gemfile and or run the bundle command:
gem "cancan"
Continue with using the generator for creating the Ability class for a User:
rails g cancan:ability
Simple as that. You can find out more here.
Now how should you model your User and Admin accounts? You can do this with a single user class, or you can do it with different classes :).
I personally prefer to use different classes to keep the two roles separately.