Ruby on rails 在Authlogic中强制验证空白密码

Ruby on rails 在Authlogic中强制验证空白密码,ruby-on-rails,authlogic,reset-password,Ruby On Rails,Authlogic,Reset Password,我正在向使用Authlogic的Rails应用程序添加密码重置功能。我是按照这里的指南来做的:除了一件事之外,所有的事情都按照我的意愿进行:密码重置表单接受空白密码,并且不会改变它们 我一直在四处搜索,了解到这是预期的默认行为,因为它允许用户编辑表单,只有在用户输入新密码时才更改用户的密码,否则将忽略它。但在本例中,我特别希望在用户最初注册时强制验证密码。对于这个问题,我已经找到了两种可能的解决方案,但还没有弄清楚如何实现这两种方案 1) 有人对谷歌集团提出了同样的问题: Ben的反应是使用@

我正在向使用Authlogic的Rails应用程序添加密码重置功能。我是按照这里的指南来做的:除了一件事之外,所有的事情都按照我的意愿进行:密码重置表单接受空白密码,并且不会改变它们

我一直在四处搜索,了解到这是预期的默认行为,因为它允许用户编辑表单,只有在用户输入新密码时才更改用户的密码,否则将忽略它。但在本例中,我特别希望在用户最初注册时强制验证密码。对于这个问题,我已经找到了两种可能的解决方案,但还没有弄清楚如何实现这两种方案

1) 有人对谷歌集团提出了同样的问题:

Ben的反应是使用
@user.validate_password=true
强制验证密码。我尝试了这个方法,但得到了一个未定义的方法错误:
未定义的方法'validate_password_field='for#

2) 似乎有一个名为
ignore\u blank\u passwords
的Authlogic配置选项。这里有记录:

这看起来是可行的,但我的理解是,这是一个全局配置选项,您可以在用户模型中的初始
acts\u asu\u authentic
调用中使用,我不想在应用程序范围内更改它,因为我有一个供用户使用的常规编辑表单,我希望默认情况下忽略空白密码


有人找到了解决办法吗?我在Authlogic 1.4.1的更改日志中看到了
validate\u password=
,从那时起,它就没有被删除过。我只是用错了吗?是否有办法在每个请求的基础上使用
忽略\u blank\u密码?

可能会测试控制器中的参数值?(航空代码):


除了Zettic的解决方案之外,您还可以这样做:

def update
  @user.password = params[:user][:password]
  @user.password_confirmation = params[:user][: password_confirmation]

  if @user.changed? && @user.save
    flash[:notice] = "Password successfully updated"
    redirect_to account_url
  else
    render :action => :edit
  end
end

您基本上是在检查authlogic是否更改了用户记录(如果密码为空,则不会更改)。在else块中,您可以检查密码是否为空,并在用户记录中添加相应的错误消息或显示一条快闪消息。

这是一种旧线程,但由于它没有应答,我将发布此消息

与其他解决方案相比,我已经做到了更干净一点,用自己的解决方案“帮助”authlogic验证

我将此添加到用户:

class User < ActiveRecord::Base

  ...

  attr_writer :password_required

  validates_presence_of :password, :if => :password_required?

  def password_required?
    @password_required
  end

  ...
end
这将产生局部效应;应用程序的其余部分不会受到影响(除非在其他地方将
password\u required
设置为true)

我希望有帮助

User.ignore_blank_passwords = false
使用模型而不是对象来设置此属性

def update_passwords
  User.ignore_blank_passwords = false
  if @user.update_attributes(params[:user])
    ...
  end
  User.ignore_blank_passwords = true
end
这就是我所做的

class User < ActiveRecord::Base
  attr_accessor :ignore_blank_passwords

  # object level attribute overrides the config level
  # attribute
  def ignore_blank_passwords?
    ignore_blank_passwords.nil? ? super : (ignore_blank_passwords == true)
  end
end

在这里,您是在AuthLogic的范围内工作的。您不必更改验证逻辑。

这不会更改整个模型的验证逻辑吗?我只想在一个操作中忽略空白密码。我可以使用
ignore\u blank\u paswords
属性。请看我的答案以了解详细信息。
def update_passwords
  User.ignore_blank_passwords = false
  if @user.update_attributes(params[:user])
    ...
  end
  User.ignore_blank_passwords = true
end
class User < ActiveRecord::Base
  attr_accessor :ignore_blank_passwords

  # object level attribute overrides the config level
  # attribute
  def ignore_blank_passwords?
    ignore_blank_passwords.nil? ? super : (ignore_blank_passwords == true)
  end
end
user.ignore_blank_passwords = false