要禁用密码确认you can simply remove the 100 field from the registration form.,则无需完全确认密码!
rails g devise:views
app\views\devise\registrations\new.html.erb
中的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 %>
这项工作的原因在于设计来源:
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_confirmation
字段为nil
,则password_required?
将返回false
.
因为如果表单中存在password_confirmation
字段,它将始终作为空字符串包含在参数散列中,如果保留为空,则会触发验证.但是,如果从表单中删除输入,参数中的password_confirmation
将是nil
,因此不会触发验证.