我正在使用带有rails 3的authlogic。我的用户模型中有这个:
validates :last_name, :presence => true
acts_as_authentic do |c|
c.validates_length_of_password_field_options = {:minimum => 7}
end
然后我有一个 Controller 操作来更新用户名:
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
如果用户输入空白姓氏并尝试使用此 Controller 操作更新其姓名,则 @current_user 模型正确地在姓氏上有错误,但在密码上也有错误(密码必须至少包含 7 个字符) 。如何仅在密码更新时验证密码?
最佳答案
您需要使用 merge_validates_* 配置方法而不是 validates_* 方法。前者保留条件(例如忽略空白密码),后者覆盖它们。那应该清除一切。并且不要在 merge_* 方法上使用赋值。
a.merge_validates_length_of_password_field_options :minimum => 7
关于ruby-on-rails - Authlogic:如何仅在密码更新时验证密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5074131/