我正在使用Devise for Rails。在默认注册过程中,Devise要求用户键入两次密码以进行验证和身份验证。我该如何禁用它?
答案 0 :(得分:84)
要停用密码确认,您只需从注册表单中删除password_confirmation
字段。这样就无需完全确认密码!
rails g devise:views
password_confirmation
app\views\devise\registrations\new.html.erb
部分
醇>
一种方法:
<%# 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 %>
这可行的原因在于设计源中的lib/devise/models/validatable.rb
:
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_required?
返回true
时才会触发验证,如果password_required?
字段为false
,password_confirmation
将返回nil
。
因为表单中存在password_confirmation
字段,所以它将始终包含在参数哈希中,如果空字符串为空字符串,则会触发验证。但是,如果您从表单中删除输入,则参数中的password_confirmation
将为nil
,因此不会触发验证。
答案 1 :(得分:36)
看起来如果你只是从模型中删除了attr_accessible要求,没有它就可以正常工作。
另一方面,我同意这种做法,在极少数情况下会出现拼写错误,用户只需使用密码恢复功能即可恢复密码。
答案 2 :(得分:11)
我不熟悉Devise但是如果您在保存/验证之前可以访问控制器中的模型,那么您可以执行以下操作
model.password_confirmation = model.password
model.save
答案 3 :(得分:3)
为了找到此问题的Rails 4用户,只需从:password_confirmation
中声明的允许的参数中删除ApplicationController.rb
。
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
答案 4 :(得分:2)
最简单的解决方案:
从
中删除:validatabledevise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:confirmable, :timeoutable, :validatable
)
答案 5 :(得分:2)
您只需要从表单中删除password_confirmation字段。
答案 6 :(得分:1)
见维基
def update_with_password(params={})
params.delete(:current_password)
self.update_without_password(params)
end
答案 7 :(得分:0)
设计默认验证( 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 字段。
答案 8 :(得分:0)
我认为这是禁用密码确认的简单方法: https://github.com/plataformatec/devise/wiki/Disable-password-confirmation-during-registration
有些用户希望缩短注册流程。 可以删除的字段之一是密码确认。
最简单的解决方案是:您只需删除password_confirmation即可 来自登记表格的字段位于 设计/注册/ new.html.erb(new.html.haml如果您正在使用 HAML),无需完全确认密码!
原因在于lib / devise / models / validatable.rb 设计来源:
请注意,只有在password_required时才会触发验证? 返回true,password_required?如果是,将返回false password_confirmation字段为nil。
因为表单中存在password_confirmation字段, 它将始终包含在参数哈希中,作为空字符串 如果留空,则触发验证。但是,如果你 从表单中删除输入,在中删除password_confirmation params将为零,因此验证不会 触发。