Ruby on rails 利用改革设计更新用户密码
在我的应用程序中,我同时使用了和Gems 我想让一个登录的用户通过提供他们的当前密码、新密码和新密码确认来非常简单地更改他们的密码。我已经实现了密码更新功能,但是我正在努力:当前的密码验证() 在我的控制器中,我使用的是:Ruby on rails 利用改革设计更新用户密码,ruby-on-rails,ruby-on-rails-4,devise,reform,Ruby On Rails,Ruby On Rails 4,Devise,Reform,在我的应用程序中,我同时使用了和Gems 我想让一个登录的用户通过提供他们的当前密码、新密码和新密码确认来非常简单地更改他们的密码。我已经实现了密码更新功能,但是我正在努力:当前的密码验证() 在我的控制器中,我使用的是: def update_password_self if @user.validate( params[:user] ) @user.save # Sign in the user by passing validation in case their pa
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
end
class ValidateThisPasswordValidator record.errors[attribute]我已经找到了解决方案。如果其他人遇到此问题,以下是解决方案:
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
end
class ValidateThisPasswordValidator