如何绕过使用Devise和更新进行重新登录

Vas*_*rth 4 authentication passwords ruby-on-rails devise

标题有点令人困惑所以我会解释.我有以下控制器方法:

  def password_update
      @op = params[:old_password]
      @np = params[:new_password]
      @cp = params[:confirm_password]

      if @np == @cp
        if !@np.empty?
          if current_user.update_with_password(:current_password=> @op)
              current_user.password = @np
              if current_user.save
                flash[:notice] = "Password Successfully Changed"
                redirect_to settings_path and return 
              end 
          else
            flash[:notice] = "Incorrent Current Password"
            redirect_to change_password_path and return 
          end
        else
          flash[:notice] = "New Password Cannot Be Blank"
        end   
      elsel
        flash[:notice] = "Incorrect Password Confirmation"
      end
      redirect_to change_password_path
    end
Run Code Online (Sandbox Code Playgroud)

其他所有工作都很好,这意味着我有工作路线和视图,可以引导您使用此方法并在表单提交时调用它.但是,当我尝试正确更改密码时,会出现错误.顺便说一下,我正在使用Devise.当我点击提交时,我会退出并显示"您必须登录才能完成此操作".所以我尝试登录,我当前的密码不起作用.它已经改变了我的密码(我在表格中设置的密码)!它告诉我,我必须登录(当我尝试更改密码时,我就是这样),但它仍然会更改它.

欢迎任何帮助,但是,我是一个新手,非常感谢详细的解释.谢谢!

cr0*_*IAN 8

我相信Devise wiki中的这个页面回答了你的问题:https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-password

特别是这段代码

 if @user.update_attributes(params[:user])
   # Sign in the user by passing validation in case his password changed
   sign_in @user, :bypass => true
   redirect_to root_path
 else
   render "edit"
 end
Run Code Online (Sandbox Code Playgroud)

旁路选项似乎也很好.希望这可以帮助.干杯