【问题标题】:Devise update user Password using Reform使用改革设计更新用户密码
【发布时间】:2015-04-11 11:04:47
【问题描述】:

在我的应用中,我同时使用了 DeviseReform Gems。

我想让登录用户通过提供当前密码、新密码和新密码确认来非常简单地更改密码。我已经实现了密码更新功能,但我正在努力解决 :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 Form 类中,我有以下内容:

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

current_user 对象不可用于表单对象,因此返回错误。使用这两个 GEMS 实现上述目标的最佳方法是什么?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 devise reform


    【解决方案1】:

    我已经找到了解决方案。万一其他人遇到这个问题,这里是解决方案:

    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
    

    【讨论】:

      猜你喜欢
      • 2011-07-04
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 2014-10-23
      • 1970-01-01
      • 2012-11-26
      相关资源
      最近更新 更多