【问题标题】:Authlogic: How can I only validate the password if the password is being updated?Authlogic:如果密码正在更新,我如何才能验证密码?
【发布时间】:2011-02-22 04:51:35
【问题描述】:

我正在使用带有 rails 3 的 authlogic。我的用户模型中有这个:

 validates :last_name, :presence => true

  acts_as_authentic do |c|
    c.validates_length_of_password_field_options = {:minimum => 7}
  end

然后我有一个更新用户名的控制器操作:

  def update_name
    if @current_user.update_attributes(params[:user])
      flash[:success_name] = true
      redirect_to edit_user_via_account_settings_path
    else
      render 'edit_user_via_account_settings'
    end
  end

如果用户输入空白姓氏并尝试使用此控制器操作更新其姓名,@current_user 模型正确地有姓氏错误,但它也有密码错误(密码必须至少为 7 个字符) .如何仅在密码正在更新时才验证密码?

【问题讨论】:

  • 这是 authlogic 的默认行为——所以我认为还有其他事情发生

标签: ruby-on-rails authlogic


【解决方案1】:

您需要使用 merge_validates_* 配置方法而不是 validates_* 方法。前者保留条件(如忽略空白密码),后者覆盖它们。那应该清除一切。并且不要在 merge_* 方法上使用赋值。

a.merge_validates_length_of_password_field_options :minimum => 7 

【讨论】:

    【解决方案2】:

    我认为您正在寻找的配置在这里找到

    http://rdoc.info/github/binarylogic/authlogic/master/Authlogic/ActsAsAuthentic/Password/Config#ignore_blank_passwords-instance_method

    除非是新用户,否则如果未提供密码,则不会验证密码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2015-02-19
      • 2023-03-31
      • 1970-01-01
      • 2018-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多