使用设计时,在注册期间禁用密码确认

Vic*_*Lam 41 ruby-on-rails registration devise

我正在使用Devise for Rails.在默认注册过程中,Devise要求用户键入两次密码以进行验证和身份验证.我该如何禁用它?

谢谢大家.:)

mis*_*tim 84

我参加这个派对有点晚了,但是在最后一个小时左右我一直在摸着同样的问题,我想我会分享我找到的最简单的解决方案:你可以简单地password_confirmation从注册表格,无需完全确认密码!


原因在于rails g devise:views设计源:

<%# 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 %>
Run Code Online (Sandbox Code Playgroud)

请注意,验证仅在password_confirmation返回时触发app\views\devise\registrations\new.html.erb,如果该字段为lib/devise/models/validatable.rb则将返回.password_required?truepassword_required?

因为false字段存在于表单中,它将始终包含在参数哈希中,如果空字符串为空字符串,则会触发验证.但是,如果从表单中删除输入password_confirmation,则参数将在nil,因此不会触发验证.

希望这很有用!

谢谢,

蒂姆


Jef*_*key 36

看起来如果你只是从模型中删除attr_accessible要求它没有它就可以正常工作.

另外,我同意这种做法,在极少数情况下会出现拼写错误,用户可以简单地使用密码恢复来恢复密码.

  • 这应该是正确的答案.假设您使用user.rb作为您的设计模型,只需删除password_confirmation的attribute_accessible并删除视图中的字段. (11认同)

Wil*_*ill 11

我不熟悉Devise但是如果您在保存/验证之前可以访问控制器中的模型,则可以执行以下操作

model.password_confirmation = model.password
model.save
Run Code Online (Sandbox Code Playgroud)