ruby on rails-设计:在注册期间禁用密码确认
我正在使用Devise for Rails。 在默认注册过程中,Devise要求用户输入两次密码以进行验证和身份验证。 如何禁用它?
要禁用密码确认,您只需从注册表单中删除password_confirmation
字段即可。 这样就无需完全确认密码!
- 尚未生成设计视图:
password_confirmation
- 卸下
password_confirmation
中的password_confirmation
部分
一种方法:
<%# Disable password confirmation so that the user doesn't have to enter a password twice %>
<% if false %>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
</div>
<% end %>
起作用的原因在于Devise来源中的password_confirmation
:
module Devise
module Models
module Validatable
def self.included(base)
base.class_eval do
#....SNIP...
validates_confirmation_of :password, :if => :password_required?
end
end
#...SNIP...
def password_required?
!persisted? || !password.nil? || !password_confirmation.nil?
end
end
end
end
请注意,只有在password_confirmation
返回password_confirmation
时,才会触发验证,而如果password_confirmation
字段为nil
,则nil
将返回false
。
因为表单中存在password_confirmation
字段,所以它将始终包含在参数hash中,如果为空字符串(如果保留为空白),则会触发验证。 但是,如果从窗体中删除输入,则参数中的password_confirmation
将为nil
,因此将不会触发验证。
看来,如果您只是从模型中删除attr_accessible要求,那么没有它就可以正常工作。
附带一提,我同意这种做法,在极少数情况下会出现错字,用户可以简单地使用密码恢复来恢复其密码。
我不熟悉Devise,但是如果您在保存/验证之前可以访问控制器中的模型,可以执行以下操作
model.password_confirmation = model.password
model.save
为了使Rails 4用户找到这个问题,只需从您在ApplicationController.rb
中声明的允许参数中删除:password_confirmation
。
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) do |u|
u.permit(:username, :email, :password)
end
devise_parameter_sanitizer.for(:account_update) do |u|
u.permit(:username, :email, :password)
end
end
最简单的解决方案:
删除:validatable从
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:confirmable, :timeoutable, :validatable
;)
您只需要从表单中删除password_confirmation字段即可。
参见维基
def update_with_password(params={})
params.delete(:current_password)
self.update_without_password(params)
end
[HTTPS://GitHub.com/普拉塔format EC/devise/wiki/how-to:-allow-users-to-edit-their-account-without-providing-啊-password]
Devise的默认验证(lib / devise / models / validatable.rb):
validates_confirmation_of :password, :if => :password_required?
和方法:
def password_required?
!persisted? || !password.nil? || !password_confirmation.nil?
end
我们需要覆盖Devise默认密码验证。将以下代码放在末尾,以免被Devise自己的任何设置覆盖。
validates_confirmation_of :password, if: :revalid
def revalid
false
end
您的模型将如下所示:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:confirmable, :timeoutable, :validatable
validates_confirmation_of :password, if: :revalid
def revalid
false
end
end
然后从注册表单中删除password_confirmation字段。
我认为这是禁用密码确认的简单方法:[https://github.com/plataformatec/devise/wiki/Disable-password-confirmation-during-registration]
一些用户希望简化注册过程。 可以删除的字段之一是密码确认。
最简单的解决方案是:您只需删除password_confirmation 注册表中的字段位于 devise / registrations / new.html.erb(如果正在使用,则为new.html.haml HAML),从而无需完全确认密码!
这样做的原因在于该目录下的lib / devise / models / validatable.rb。 设计来源:
请注意,仅在需要password_required时才触发验证。 返回true,并且需要password_required? 如果 password_confirmation字段为nil。
因为在表单中存在password_confirmation字段, 它将始终以空字符串的形式包含在参数hash中 如果为空,则触发验证。 但是,如果你 从表单中的输入中删除密码password_confirmation 参数将为nil,因此验证不会为 触发。