Resque无法识别实例变量

时间:2015-08-14 04:32:53

标签: ruby-on-rails resque

我正在使用resque发送电子邮件。如果我的邮件程序模板包含对实例变量的任何引用,则不会发送电子邮件。在使用resque将激活邮件移动到后台作业之前,激活邮件工作正常。

users_controller.rb

def create
  @user = User.new(user_params)
  if @user.save
    Resque.enqueue(SendActivationEmail, @user.id)
    flash[:info] = "Please check your email to activate your account."
    redirect_to root_url
  else
    render 'new'
  end
end

send_activation_email.rb

class SendActivationEmail
  @queue = :email_queue
  def self.perform(user_id)
    user = User.find(user_id)
    UserMailer.account_activation(user).deliver_now
  end
end

user_mailer.rb

class UserMailer < ApplicationMailer
  def account_activation(user)
    @new_user = user
    mail to: user.email, subject: "Account activation"
  end
end

account_activation.text.erb

Hi,

Welcome. Click on the link below to activate your account:

<%= edit_account_activation_url(@new_user.activation_token, email: @new_user.email) %>

resque web中记录的错误是:     没有路由匹配{:action =&gt;“edit”,:controller =&gt;“account_activations”,:email =&gt;“user5@domain.com”,:id =&gt; nil}缺少必需的密钥:[:id]

删除链接时会发送以下信息......

account_activation.text.erb

Hi,

Welcome. Click on the link below to activate your account:

路线正确:

edit_account_activation GET    /account_activations/:id/edit(.:format)                             account_activations#edit

2 个答案:

答案 0 :(得分:0)

只是为了确保,稍微更改您的user_mailer.rb以使用@new_user实例变量以account_activation方法检索它的电子邮件。

class UserMailer < ApplicationMailer
  def account_activation(user)
    @new_user = user
    mail to: @new_user.email, subject: "Account activation"
  end
end

config/environments/development.rb文件中,添加:

config.action_mailer.raise_delivery_errors = true

如果电子邮件递送中有任何错误,这将通知您。您将能够查看您的开发日志。

尝试这两件事,让我知道更新。我会在收到您的反馈后更新我的答案。

更新

当你说:The activation mailer worked correctly before I moved it to a background job using resque.你的意思是什么?你在生产模式下测试了吗?还是处于开发模式?在开发模式下,我认为没有发送实际的电子邮件,但您可以预览它(在Rails 4.1.x中)。当您进入生产环境时,您将需要设置外部服务器以发送实际的电子邮件。

但是,添加:

config.action_mailer.raise_delivery_errors = true
根据我上面的建议,在您的config/environments/development.rb file

,然后您就可以看到电子邮件投放中是否有任何错误。然后,您可以采取下一步。希望这会有所帮助。

答案 1 :(得分:0)

问题是实例变量从基于符号的键转换为基于字符串的键。在我的邮件视图中,我按符号获取值,因此可以通过字符串获取值,或者调用hash.symbolize_keys!转换回符号键。

我找到的解决方案来自:https://github.com/zapnap/resque_mailer/issues/23