使用devise时注册时禁用密码确认

我正在使用Devise for Rails。 在默认注册过程中,Devise要求用户input密码两次以进行validation和authentication。 我怎样才能禁用它?

谢谢大家。 🙂

我对这个派对有点迟,但是在过去的一个小时左右,我只是一直在思考同样的问题,我以为我会分享我find的最简单的解决scheme: 你可以简单地将password_confirmation字段从registry格,它禁用了完全确认密码的必要!


其原因在于Devise源代码中的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?才会触发password_required? 返回true ,并且password_required? 如果password_confirmation字段nil将返回false

因为password_confirmation字段存在于表单中,所以它将始终被包含在参数hash中,如果它是空的,则作为一个空string被触发。 但是,如果从表单中删除input,则参数中的password_confirmationnil ,因此validation将不会被触发。

希望这是有用的!

谢谢,

蒂姆

看来,如果你只是从模型中删除attr_accessible要求,它没有它的工作就好了。

在一个侧面说明,我同意这种做法,在极less数情况下,有一个错字,用户可以简单地使用密码恢复来恢复他们的密码。

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

 model.password_confirmation = model.password model.save 

为了find这个问题的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 

最简单的解决scheme

删除:validatable

 devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :confirmable, :timeoutable, :validatable 

;)

看维基

 def update_with_password(params={}) params.delete(:current_password) self.update_without_password(params) end 

https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

您只需要从表单中删除password_confirmation字段。

devise的默认validation( lib / devise / models / validatable.rb ):

 validates_confirmation_of :password, :if => :password_required? 

和方法:

 def password_required? !persisted? || !password.nil? || !password_confirmation.nil? end 

我们需要重写devise默认的密码validation。 把下面的代码放在最后,以免被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 

然后从registry单中删除password_confirmation字段。

我认为这是禁用密码确认的简单方法: https : //github.com/plataformatec/devise/wiki/Disable-password-confirmation-during-registration

一些用户希望使注册过程更短,更容易。 可以删除的字段之一是密码确认。

最简单的解决scheme是:只需从位于devise / registrations / new.html.erb的registry单(如果您使用的是HAML,则为new.html.haml)中删除password_confirmation字段,即禁用需要完全确认密码!

其原因在于Devise源代码中的lib / devise / models / validatable.rb:

请注意,只有在password_required时才会触发validation? 返回true,并且password_required? 如果password_confirmation字段为零,将返回false。

因为password_confirmation字段存在于表单中,所以它将始终被包含在参数hash中,如果它是空的,则作为一个空string被触发。 但是,如果从表单中删除input,则参数中的password_confirmation将为零,因此validation将不会被触发。