基本使用
在Gemfile里面添加gem 'devise'
運行bundle install
然后安裝devise相關(guān)組件锈遥,rails generate devise:install
安裝完提示如下卖鲤,按照如下設(shè)置:
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
===============================================================================
2.執(zhí)行命令rails g devise:views渡蜻,生成devise的視圖文件
3.生成需要用到devise的模型晚吞,rails g devise user娩缰,運行了這條命令之后惭蹂,路由中會自動生成一個devise_for :users,
rake db:migrate,執(zhí)行遷移文件
4.新建一個控制器熙含,rails generate controller home index罚缕,用來設(shè)置root頁面
5.在控制器中,設(shè)置訪問之前需要先登錄
class HomeController < ApplicationController
before_action :authenticate_user!, :only => [:index, :new]
6.devise會默認創(chuàng)建一個幫助方法:
before_action :authenticate_user!
user_signed_in? //判斷用戶是否登錄
current_user //獲取當(dāng)前登錄用戶
user_session //可以訪問對應(yīng)的session
7.登錄之后怎静,devise會有默認跳轉(zhuǎn)到root邮弹,如果想自定義跳轉(zhuǎn),需要覆蓋after_sign_in_path_for和after_sign_out_path_for來自定義跳轉(zhuǎn)回調(diào)蚓聘。
8.devise除了用emails登錄之外腌乡,自定義登錄字段
rails generate migration add_username_to_users username:string
增加字段后,執(zhí)行rake db:migrate
9.增加了新字段之后夜牡,在config/initializers/devise.rb中配置登錄驗證的字段
config.authentication_keys = [:username]
config.case_insensitive_keys = [:username]
config.strip_whitespace_keys = [:username]
10.修改完字段后与纽,對應(yīng)的去修改views里面的視圖,把登錄頁面的email字段改成username塘装,在注冊頁面新加上username的input
11.修改完之后急迂,接下來需要重寫一個方法,用來配置登錄和注冊所允許的參數(shù)蹦肴,將此方法寫在application_controller.rb中
def configure_permitted_parametersod_name
devise_parameter_sanitizer.permit(:sign_in) {|u| u.permit(:email, :username)}
devise_parameter_sanitizer.permit(:sign_up) {|u|
u.permit(:email, :username, :password, :password_confirmation)}
end
在application_controller.rb還需要配置
before_action :configure_permitted_parametersod_name, if: :devise_controller?
如果沒有配置這個僚碎,在注冊的時候,會出現(xiàn)郵箱驗證不通過的BUG
13.為了使得用戶名和郵箱都可以登錄阴幌,需要在user模型里加入一個
虛擬屬性:
attr_accessor :signin
然后在/config/ initializers/devise.rb中修改驗證參數(shù)
config.authentication_keys = [ :signin ]
14.修改了驗證參數(shù)之后听盖,需要去模型里面重寫登錄devise會使用到的方法,在user.rb里面重寫self.find_for_database_authentication方法
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if signin = conditions.delete(:signin)
where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => signin.downcase }]).first
elsif conditions.has_key?(:username) || conditions.has_key?(:email)
where(conditions.to_h).first
end
end
15.在模型里定義完方法之后,需要將application_controller.rb中那個允許參數(shù)的方法重寫
def configure_permitted_parametersod_name
devise_parameter_sanitizer.permit(:sign_in) {|u| u.permit(:signin, :password, :remember_me)}
devise_parameter_sanitizer.permit(:sign_up) {|u|
u.permit(:email, :username, :password, :password_confirmation)}
end
16.去登錄視圖中裂七,將username的input改成signin的input,這樣一來皆看,用用戶名和郵箱都可以實現(xiàn)登錄功能
17.編輯用戶資料的時候,需要允許用戶修改用戶名背零,郵箱腰吟,和密碼
首先在編輯頁面加入username的input飞崖,方便修改用戶名
<div>
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
18.接下來要做的事情是去修改devise的邏輯弧圆,新建一個注冊控制器叫做registrations_controller
rails generate controller registrations update
19.生成控制器后,更改update方法玄妈,在控制器registrations_controller中將update方法定義如下:
def update
new_params = params.require(:user).permit(:email,
:username, :current_password, :password,
:password_confirmation)
@user = User.find(current_user.id)
if @user.update_with_password(new_params)
set_flash_message :notice, :updated
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end
end
在健壯參數(shù)中加入username等屬性侦镇,然后用update_with_password
方法更新參數(shù)(這個方法是devise定義的)
20.在更改過控制器之后灵疮,需要更改路由,使得修改賬戶的時候壳繁,默認跳到我們新加的registrations_controller控制器中震捣,路由修改方式如下:
devise_for :users, :controllers => {:registrations =>
"registrations"}
未完待續(xù)