是否可以让AuthLogic验证密码确认(如果存在),但是否则忽略它?所以,如果我的PARAMS表现如下,我想失败:可选密码确认Authlogic
{ user: { password: "abcd", password_confirmation: "defg" }
但是,如果参数,而不是为下面我想成功的:
{ user: { password: "abcdefgh" } }
谢谢!
是否可以让AuthLogic验证密码确认(如果存在),但是否则忽略它?所以,如果我的PARAMS表现如下,我想失败:可选密码确认Authlogic
{ user: { password: "abcd", password_confirmation: "defg" }
但是,如果参数,而不是为下面我想成功的:
{ user: { password: "abcdefgh" } }
谢谢!
它不是一个AuthLogic的事情,这个代码应该做的伎俩:
class User < ActiveRecord::Base
...
def update_without_password_confirmation(params={})
params.delete(:password) if params[:password_confirmation].blank?
params.delete(:password_confirmation) if params[:password].blank?
update_attributes(params)
end
end
然后调用来自控制器此方法时,你需要更新用户属性。如果用户没有指定password_confirmation,它将被忽略。 您可以使用其他几种技术来获得相同的效果,例如,使用before_validation(:on =>:update)回调。
UPDATE:如果你想跳过所有密码验证,acts_as_authentic接受块来定制其行为:
clas User < AR::Base
acts_as_authentic do |u|
u.require_password_confirmation=false # you can also use :if => some_condition
u.validate_password_field=false
end
end
这有点未记载:P检查这个文件: https://github.com/binarylogic/authlogic/blob/master/lib/authlogic/acts_as_authentic/password.rb
before_validate :check_password_confirmation
def check_password_confirmation
if self.password_confirmation.blank?
self.password = nil
end
end
有了额外的好处,您不必担心在控制器中设置或取消设置 - 模型会继续正常update_attributes。
我永远不知道为什么这得到downvoted。 – phillmv 2012-09-26 22:10:54
任何方式来切换它,使一般的'更新'或'创建'将工作?我真的很喜欢这个应用程序。 – Stussa 2011-06-01 00:22:26
好吧,更新我的答案,更好的解释;) – 2011-06-01 12:32:23