在我的应用程序中,我同时使用了Devise和Reform Gem。
我想要使登录用户能够非常简单地更改他们的密码,通过提供他们的当前密码,新密码和新密码确认。我已经实现了密码更新功能,但我正在为:current_password验证( Wiki used for help)而苦苦挣扎。
在我的控制器中,我使用的是:
def update_password_self
if @user.validate( params[:user] )
@user.save
# Sign in the user by passing validation in case their password changed
sign_in @user.model, :bypass => true
respond_with(@user, :location => user_self_platform_profile_path( id: @user.id, anchor: "#user_self_settings"))
else
render "edit_password_self"
end
end在我的UserForm窗体类中,我有以下内容:
property :current_password,
validates: {
presence: true,
validate_this_password: true
}验证此密码是一个自定义验证器,如下所示:
class ValidateThisPasswordValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless current_user.valid_password?(value)
record.errors[attribute] << (options[:message] || "password not right.")
end
end
endcurrent_user对象不可用于form对象,因此返回错误。使用这两个GEMS实现上述目标的最佳方式是什么?
谢谢
发布于 2015-04-11 21:24:57
我已经想出了解决办法。如果其他人遇到这个问题,这里是解决方案:
class ValidateThisPasswordValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless User.find(record.id).valid_password?(value)
record.errors[attribute] << (options[:message] || "password not right.")
end
end
endhttps://stackoverflow.com/questions/29577144
复制相似问题