项目作者: SixArm

项目描述 :
SixArm.com » Rails » Demo Devise
高级语言: Ruby
项目地址: git://github.com/SixArm/sixarm_rails_demo_devise.git
创建时间: 2015-01-15T00:08:30Z
项目社区:https://github.com/SixArm/sixarm_rails_demo_devise

开源协议:

下载


SixArm » Rails »
Demo Devise authentication

Rails demonstration application that shows how to set up the Devise gem for user authentication.

Start

Create a new Rails application:

  1. rails new demo_devise --database=postgresql --skip-bundle
  2. cd demo_devise

Bundle:

  1. bundle install --path vendor/bundle --binstubs vendor/bundle/binstubs
  2. echo /vendor >> .gitignore

Initialize the database:

  1. bin/rake db:create db:migrate

Test:

  1. bin/rake test

Launch

Start the Rails server:

  1. bin/rails server

Browse http://localhost:3000 and you see the application running.

Home Page

Edit config/routes.rb and change the root path to this:

  1. root 'home#index'

Generate:

  1. bin/rails generate controller home index

Browse http://localhost:3000 to see the new home page.

Messages

Edit app/views/layouts/application.html.erb and insert these lines after the body tag:

  1. <body>
  2. <p class="notice"><%= notice %></p>
  3. <p class="alert"><%= alert %></p>

Mailers

Edit config/environments/development.rb and add this:

  1. # Set a default host that will be used in all mailers.
  2. config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

Do the same for test.rb and production.rb for now; you will need to change these later.

Generate User

Generate a user:

  1. rails generate resource user name:string
  2. bin/rake db:migrate

Devise

Edit Gemfile:

  1. # Use Devise for user authentication
  2. gem 'devise'

Bundle:

  1. bundle

Verify config/environments/development.rb sets the mailer default, such as:

  1. config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

Verify app/views/layouts/application.html.erb shows messges, such as:

  1. <p class="notice"><%= notice %></p>
  2. <p class="alert"><%= alert %></p>

Verify config/routes.rb has a root line, such as:

  1. root 'home#index'

Generate:

  1. rails generate devise:install
  2. rails generate devise user
  3. bin/rake db:migrate

Edit test/fixtures/users.yml and add a unique email address to each user:

  1. one:
  2. name: MyString
  3. email: user1@example.com
  4. two:
  5. name: MyString
  6. email: user2@example.com

Devise upgrades

Devise upgrades the application and user model with these capabilities:

  • email
  • encrypted password
  • sign in tracking
  • confirmable accounts
  • lockable accounts

The db/schema.rb users section looks like this:

  1. create_table "users", force: :cascade do |t|
  2. t.string "name"
  3. t.datetime "created_at", null: false
  4. t.datetime "updated_at", null: false
  5. t.string "email", default: "", null: false
  6. t.string "encrypted_password", default: "", null: false
  7. t.string "reset_password_token"
  8. t.datetime "reset_password_sent_at"
  9. t.datetime "remember_created_at"
  10. t.integer "sign_in_count", default: 0, null: false
  11. t.datetime "current_sign_in_at"
  12. t.datetime "last_sign_in_at"
  13. t.inet "current_sign_in_ip"
  14. t.inet "last_sign_in_ip"
  15. end
  16. add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
  17. add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

Devise authentication

Edit app/controllers/home_controller.rb and add authentication:

  1. class HomeController < ApplicationController
  2. before_action :authenticate_user!

Browse http://localhost:3000 and you now see the Devise sign in page.

The page says “You need to sign in or sign up before continuing.”

Devise sign up

Sign up:

You should see: “Welcome! You have signed up successfully.”

To see the new user record in Rails:

  1. bin/rails console
  1. > User.first.email
  2. User Load (0.7ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
  3. => "alice@example.com"