原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://kinda22.blog.51cto.com/2969503/1587991
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#在Gemfile中添加,目前版本3.4.1
gem
'devise'
#然后只需bundle install 安装它..
bundle
install
#初始化devise
rails generate devise:
install
#创建一个MODEL,指定名称..
rails generate devise user
#会生成一个迁移文件,可以对这个迁移文件做一些修改...
rake db:migrate
|
1
2
3
4
|
#在开发环境中默认的url和生产环境可能有点不太一样..
#在下面development的文件中,增加下面的一行。
config
/environments/development
.rb
config.action_mailer.default_url_options = { host:
'localhost'
, port: 3000 }
|
1
|
before_action :authenticate_user!
|
1
|
user_signed_in?
|
1
|
current_user
|
1
|
user_session
|
1
|
root to:
"home#index"
|
1
2
3
4
|
before_action :authenticate_member!
member_signed_in?
current_member
member_session
|
1
|
devise :database_authenticatable, :registerable, :confirmable, :recoverable, stretches: 2
|
1
|
config
/initializers/devise
.rb
|
1
2
3
|
sign_in (Devise::SessionController
#create) 只允许验证的keys(类似email)
sign_up (Devise::RegistrationsController
#create) 允许验证的keys: password和password_confirmation
account_update (Devise::RegistrationsController
#update) 允许验证的keys: password , password_confirmation 和 current_password
|
1
2
3
4
5
6
7
8
|
class ApplicationController < ActionController::Base
before_action :configure_permitted_parameters,
if
: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.
for
(:sign_up) << :username
endend
|
1
2
3
|
def configure_permitted_parameters
devise_parameter_sanitizer.
for
(:sign_in) { |u| u.permit(:username, :email) }
end
|
1
2
3
|
def configure_permitted_parameters
devise_parameter_sanitizer.
for
(:sign_up) { |u| u.permit({ roles: [] }, :email, :password, :password_confirmation) }
end
|
1
2
3
4
5
|
class User::ParameterSanitizer < Devise::ParameterSanitizer
def sign_in
default_params.permit(:username, :email)
end
end
|
1
2
3
4
5
6
7
8
9
10
|
class ApplicationController < ActionController::Base
protected
def devise_parameter_sanitizer
if
resource_class == User
User::ParameterSanitizer.new(User, :user, params)
else
super
# Use the default one
end
end
end
|
1
|
rails generate devise:views
|
1
|
rails generate devise:views
users
|
1
|
rails generate devise:views -
v
registrations confirmations
|
1
|
rails generate devise:controllers [scope]
|
1
2
3
4
5
6
7
|
class Users::SessionsController < Devise::SessionsController
# GET /resource/sign_in
# def new
# super
# end
...
end
|
1
|
devise_for :
users
, controllers: { session:
"users/sessions"
}
|
1
2
3
4
5
|
class Users::SessionsController < Devise::SessionsController
def create
# custom sign-in code
end
end
|
1
2
3
4
5
|
class Users::SessionsController < Devise::SessionsController
def create
super
do
|resource| BackgroundWorker.trigger(resource) end
end
end
|
1
|
devise_for :
users
, path:
"auth"
, path_names: { sign_in:
'login'
, sign_out: }
|
1
2
3
|
devise_scope :user
do
get
"sign_in"
, to:
"devise/sessions#new"
end
|
1
2
3
4
|
en:
devise:
sessions:
signed_in:
'Signed in successfully.'
|
1
2
3
4
5
6
7
|
en:
devise:
sessions:
user:
signed_in:
'Welcome user, you are signed in.'
admin:
signed_in:
'Hello admin!'
|
1
2
3
4
5
6
7
8
|
en:
devise:
mailer:
confirmation_instructions:
subject:
'Hello everybody!'
user_subject:
'Hello User! Please confirm your email'
reset_password_instructions:
subject:
'Reset instructions'
|