NoMethodError - undefined method `reset_password_token' for User:Class:

给你一囗甜甜゛ 提交于 2020-01-06 14:04:55

问题


In my Rails application, I have a form for admin to create normal users. In create action, I am generating a reset password token, and sending a welcome mail to the user, with a link in it to reset his password. This is my code.

@user = User.new params[:user]
@user.reset_password_token = User.reset_password_token
@user.reset_password_sent_at = Time.now.utc
if @user.save
  UserMailer.welcome_email(@user).deliver
  ..

This works fine, but I have another app with the same code, but uses devise 3.2.2 in which I get the this error.

NoMethodError - undefined method `reset_password_token' for User:Class:

I see that the method has been removed. How can I generate a reset password token and send it to a user?

Note: I don't want to send the default reset password email


回答1:


After a lot of digging into devise's source code, I got it to work by doing this.

raw, enc = Devise.token_generator.generate(User, :reset_password_token)
@user.reset_password_token = enc
@user.reset_password_sent_at = Time.now.utc
if @user.save
  UserMailer.welcome_email(@user, raw).deliver
  ..

Use raw as the reset_password_token



来源:https://stackoverflow.com/questions/21844896/nomethoderror-undefined-method-reset-password-token-for-userclass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!