Mailer unable to access reset_token in User model

懵懂的女人 提交于 2019-12-29 09:12:00

问题


faced with an issue where @user.reset_token returns nil.

app/views/user_mailer/password_reset.html.erb

<%= link_to "Reset password", edit_password_reset_url(@user.reset_token, email: @user.email) %>

Reset_token is declared in User model, whereby this problem happens when I try to use a sidekiq worker. Refer to code below.

app/models/user.rb

class User < ActiveRecord::Base

attr_accessor :reset_token

  def User.new_token
    SecureRandom.urlsafe_base64
  end  

  def send_password_reset_email
    PasswordResetWorker.perform_async(self.id)
  end  

private

  def create_reset_digest
    self.reset_token = User.new_token
    update_attribute(:reset_digest,  User.digest(reset_token))
    update_attribute(:reset_sent_at, Time.zone.now)
  end 

app/workers/password_reset_worker.rb

class PasswordResetWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform(user_id)
    user = User.find(user_id)
    UserMailer.password_reset(user).deliver
  end
end

app/mailers/user_mailer.rb

class UserMailer < ActionMailer::Base
  default from: "noreply@example.com"

  def password_reset(user)
    @user = user
    mail to: user.email, subject: "Password Reset"
  end
end

This problem DOES NOT happen when I do not use workers

app/models/user.rb

  def send_password_reset_email
    UserMailer.password_reset(self).deliver
  end

Would like to know what can I replace "@user.reset_token" with? Let me know if you need more info. Thanks in advance.


回答1:


You're not storing the reset_token in the database - you're storing the reset_digest.

When you don't use workers, you're storing the reset_token in the User instance, then passing that same User instance to your mailer - hence the reset_token is still available.

When you use workers, your worker only has the User's ID, so it's reloading the User instance from the database. Because the reset_token isn't being stored in the database, it's coming back nil.

Either you should be saving the reset_token in the database, or your password email should be using reset_digest in the URL.



来源:https://stackoverflow.com/questions/27883926/mailer-unable-to-access-reset-token-in-user-model

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