Installation
In your gem file:
gem “devise”
$ bundle install
$ rails g devise:install
After that, your config files will be at:
config/initializers/devise.rb
Time to configure the user’s model:
$ rails g devise user
$ rake db:migrate
Note that at this point, you have a set of automatic views that are internal to devise. It is advisable that you run the following command, so you can customize those yourself in your views:
rails generate devise:views
Set pages so only login users can see them
In your controller, add the following line:
before_filter :authenticate_user!, :only => [:index, :new, :destroy]
Helpers
current_user contains information about the current user. for example current_user.email
user_signed_in? boolean to test if a user is signed in or not, use it as:
<% if user_signed_in? %>
<%= link_to 'Sign Out', destroy_user_session_path, method: :delete %>
<% end %>
user_session is the session attached to the user, you can store stuff in it like:
user_session[:somevarname] = “somevarvalue”
Options
In the user’s model, confirmable takes care of sending a confirmation email to newly created accounts
:confirmable
You also need to add a migration along with the confirmable option:
class AddConfirmableToUsers < ActiveRecord::Migration
def up
add_column :users, :unconfirmed_email, :string
add_column :users, :confirmation_token, :string
add_column :users, :confirmed_at, :string
add_column :users, :confirmation_sent_at, :datetime
add_index :users, :confirmation_token, :unique => true
User.update_all(:confirmed_at => Time.now) #your current data will be treated as if they have confirmed their account
end
def down
remove_column :users, :unconfirmed_email, :confirmation_token, :confirmed_at, :confirmation_sent_at
end
end