/devise-for-rails-beginners

Step by step tutorial for new Rails learning to write a multiuser application with the Devise authentication system.

Primary LanguageShell

# Overview

[1/2] Generate an initial application

  1. rails new

    rails new appname
    
  2. Create a “home” controller and a “home/index” page

    rails generate controller home index --no-controller-specs --skip-stylesheets --skip-javascripts
    
    • –skip-stylesheets –skip-javascripts to avoid cluttering our application with stylesheet and JavaScript files we don’t need.
  3. Set the default route to home/index in config/routes.rb

    root 'home#index'
    
  4. Set the time zone <../config/application.rb>

    rake -D time
    rake time:zones:us
    
    config.time_zone = 'Pacific Time (US & Canada)'
    

[6/6] Create a User authentication system with Devise < see step-by-step-devise.org >

  1. Enable `devise` gem in Gemfile

    gem 'devise', '~> 3.0.0.rc' # Wed May  8 18:03:54 PDT 2013, Rails 4.0.0.rc1
    
  2. Run the Devise gem install generator

    rails generate devise:install
    
  3. Generate a User Model and generate routes for user activities

    rails generate devise User
    
  4. Run the devisecreateusers database migration the was created by in the previous command

    rake db:migrate
    
  5. (Re)start the Rails server

    kill -USR1 `cat ../tmp/pids/server.pid `; rails server --daemon
    
  6. Place sign up and sign out links on the home page <../app/views/home/index.html.erb>

    <h1>Home#index</h1>
    <%= Time.now %>
    <li><%= link_to "Sign Up", new_user_registration_path %></li>
    <li><%= link_to "Sign In", new_user_session_path %></li>
    <li><%= link_to "Sign Out", destroy_user_session_path, :method => 'delete' %></li>
    
    <% if user_signed_in? %>
    You are signed in, current_user.id = <%= current_user.id %><br />
    user_session.keys => <%= user_session.keys %>
    <% end %>
    

(Optional) create a user from console

User.new(:email => "user@name.com", :password => 'password', :password_confirmation => 'password').save

Resources

Devise primary sources

  1. http://rubygems.org/gems/devise

  2. https://github.com/plataformatec/devise