標籤:
(1)新增gem
gem ‘devise‘
(2)添加devise設定檔
/workspace/shop:$ rails generate devise:install User create config/initializers/devise.rb create config/locales/devise.en.yml===============================================================================Some setup you must do manually if you haven‘t yet: 1. Ensure you have defined default url options in your environments files. Here is an example of default_url_options appropriate for a development environment in config/environments/development.rb: config.action_mailer.default_url_options = { host: ‘localhost‘, port: 3000 } In production, :host should be set to the actual host of your application. 2. Ensure you have defined root_url to *something* in your config/routes.rb. For example: root to: "home#index" 3. Ensure you have flash messages in app/views/layouts/application.html.erb. For example: <p class="notice"><%= notice %></p> <p class="alert"><%= alert %></p> 4. If you are deploying on Heroku with Rails 3.2 only, you may want to set: config.assets.initialize_on_precompile = false On config/application.rb forcing your application to not access the DB or load models when precompiling your assets. 5. You can copy Devise views (for customization) to your app by running: rails g devise:views===============================================================================
(3) 添加一個user的model
/workspace/shop:$ rails generate devise User invoke active_record create db/migrate/20151026095446_devise_create_users.rb create app/models/user.rb invoke rspec create spec/models/user_spec.rb insert app/models/user.rb route devise_for :users
(4) 安裝devise視圖檔案
/workspace/shop:$ rails g devise:views invoke Devise::Generators::SharedViewsGenerator create app/views/devise/shared create app/views/devise/shared/_links.html.erb invoke form_for create app/views/devise/confirmations create app/views/devise/confirmations/new.html.erb create app/views/devise/passwords create app/views/devise/passwords/edit.html.erb create app/views/devise/passwords/new.html.erb create app/views/devise/registrations create app/views/devise/registrations/edit.html.erb create app/views/devise/registrations/new.html.erb create app/views/devise/sessions create app/views/devise/sessions/new.html.erb create app/views/devise/unlocks create app/views/devise/unlocks/new.html.erb invoke erb create app/views/devise/mailer create app/views/devise/mailer/confirmation_instructions.html.erb create app/views/devise/mailer/reset_password_instructions.html.erb create app/views/devise/mailer/unlock_instructions.html.erb
(5)產生下表
/workspace/shop:$ rake db:migrate== 20151026095446 DeviseCreateUsers: migrating ================================-- create_table(:users) -> 0.0312s-- add_index(:users, :email, {:unique=>true}) -> 0.0004s-- add_index(:users, :reset_password_token, {:unique=>true}) -> 0.0004s== 20151026095446 DeviseCreateUsers: migrated (0.0322s) =======================
(6)導覽列增加登入 註冊的入口app/views/layouts/application.html.erb
<ul class="nav navbar-nav navbar-right"> <% if user_signed_in? %> <li><%= link_to current_user.email, profile_path %></li> <li><%= link_to "退出", destroy_user_session_path, method: :delete %></li> <% else %> <li><%= link_to "登入", new_user_session_path %></li> <li><%= link_to "註冊", new_user_registration_path %></li> <% end %> </ul>
(7)修改下登入頁面的樣式
<div class="row"> <div class="col-md-6"> <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> <div class="form-group"> <%= f.label :email, class: "control-label" %> <%= f.email_field :email, autofocus: true, class: "form-control" %> </div> <div class="form-group"> <%= f.label :password, class: "control-label" %> <%= f.password_field :password, autocomplete: "off", class: "form-control" %> </div> <% if devise_mapping.rememberable? -%> <div class="form-group"> <%= f.check_box :remember_me %> <%= f.label :remember_me %> </div> <% end -%> <div class="actions"> <%= f.submit "登入", class: "btn btn-primary" %> <%= link_to "忘記密碼", new_password_path(resource_name), class: "btn btn-link" %> </div> <% end %> </div></div>
(8)為了保護我們的方法,在每個方法前加一個登入校正app/controllers/application_controller.rb
class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception before_action :authenticate_user!end
(9)對於不需要登入就可以公開的一些頁面,在controller裡增加跳過驗證的語句
class ProductsController < ApplicationController skip_before_action :authenticate_user!, only: [:index, :show]
為應用增加登入註冊功能 ruby on rails