为什么将validate_password_field设置为false会禁用所有authlogic验证?

问题描述

| 我试图允许用户使用Twitter等外部服务注册我的应用程序。因此,我不需要Authlogic尝试验证的用户模型密码。结果,我将禁用密码验证,如下所示:
acts_as_authentic
before_validation :update_authlogic_config
#In the case that the user has signed up with an omniauth service.
attr_accessor :needs_no_password
def update_authlogic_config     
    validate_password_field = !needs_no_password
end
这一切都足够好,但是它似乎也禁用了我想保留的电子邮件/用户名验证。 结果,我更新了我的方法以确保电子邮件字段的验证如下:
def update_authlogic_config
   validate_password_field = !needs_no_password
   ignore_blank_passwords = needs_no_password
   validate_email_field = true
end
使用它,它将密码验证拖回去,给我以下错误:   密码太短(最小为4   字符)      密码确认为   太短(最少4个字符) 有任何想法吗?     

解决方法

如果您未将crypted_pa​​ssword字段放入用户模型,则不会加载密码模块,因此不会运行验证。 您会发现这是Authlogic中每个模块的正常行为。     ,您正在寻找的是一种简单地忽略密码验证的方法。这是一个称为渐进式参与的过程,最常用于创建用户帐户,但不需要特定内容,直到用户真正需要它们来进行更高级的系统操作为止。您想要做的是像这样修改模型中的authlogic配置:
acts_as_authentic do |c|
  c.merge_validates_confirmation_of_password_field_options({:unless => :networked?})
  c.merge_validates_length_of_password_field_options({:unless => :networked?})
  c.merge_validates_length_of_password_confirmation_field_options({:unless => :networked?})
end

def networked?
  self.authentications.any? # or true/false boolean of some kind
end
我从以下要点获得了此信息:http://gist.github.com/436707/