如何将数据传递到通过 Sidekiq 发送的电子邮件模板
How to pass data to an email template that is sent via Sidekiq
我在此应用中使用 Rails、Mongoid 和 Sidekiq。
在我的 Mailer 中,我有以下内容:
def send_invoice(invoice, domain)
@invoice = Invoice.find(invoice)
@user = User.find(domain)
attachments["invoice.pdf"] = File.read("#{Rails.root}/invoices/pdf/55f5c596019e2b51af000000-55f82b520540a78f43000000.pdf")
mail to: "wagner.matos@mac.com", subject: "Invoice from MyJarvis", reply_to: @user.email, :from => @user.email, :bcc => 'wagner.matos@mac.com'
end
在我的电子邮件模板中有这样的内容 (haml):
!!!
%html
= "Hello #{@invoice[:name]}"
如果我使用 ActiveMailer(不使用 Sidekiq)一切正常。现在在我的工人中:
def perform(data, count)
message = JSON.load(data)
UserMailer.send_invoice(message['invoice'], message['domain']).deliver
end
然后我收到一条错误消息 ActionView::Template::Error: undefined method '[]' for nil:NilClass
经过一番调查,我发现问题出在电子邮件中使用的变量上。当我在不使用变量的情况下尝试使用相同的电子邮件时,一切正常。
现在我明白这是一个关于 Sidekiq 如何在 Redis 中存储数据的问题(如 json)。我不知道的是,如何使用 Sidekiq 将变量传递给电子邮件模板?
编辑:这是我的 Mailer 和我的 Worker:
邮寄者:
def send_invoice(invoice, domain)
@invoice = Invoice.find(invoice)
@user = User.find(domain)
attachments["invoice.pdf"] = File.read("#{Rails.root}/invoices/pdf/55f5c596019e2b51af000000-55f82b520540a78f43000000.pdf")
mail to: "email@example.com", subject: "Invoice", reply_to: @user.email, :from => @user.email
end
工人:
def perform(data, count)
message = JSON.load(data)
UserMailer.send_invoice(message['invoice'], message['domain']).deliver
end
在我的控制器中:
...
h = JSON.generate({ 'invoice' => @invoice.id.to_s, 'domain' => set_db })
PostmanWorker.perform_async(h, 1)
...
因此,如您所见,我正在传递要在工作程序内部处理的 id,而不是传递哈希。但是如何将 @invoice
散列传递给电子邮件模板?
根据文档 (https://github.com/mperham/sidekiq/wiki/Active-Job),有一个更新的语法 (API) 用于发送电子邮件:
UserMailer.welcome_email(@user).deliver_later!(wait: 1.hour)
查看 "Action Mailer" 下的部分,因为这可能会解决您的问题。
我在此应用中使用 Rails、Mongoid 和 Sidekiq。
在我的 Mailer 中,我有以下内容:
def send_invoice(invoice, domain)
@invoice = Invoice.find(invoice)
@user = User.find(domain)
attachments["invoice.pdf"] = File.read("#{Rails.root}/invoices/pdf/55f5c596019e2b51af000000-55f82b520540a78f43000000.pdf")
mail to: "wagner.matos@mac.com", subject: "Invoice from MyJarvis", reply_to: @user.email, :from => @user.email, :bcc => 'wagner.matos@mac.com'
end
在我的电子邮件模板中有这样的内容 (haml):
!!!
%html
= "Hello #{@invoice[:name]}"
如果我使用 ActiveMailer(不使用 Sidekiq)一切正常。现在在我的工人中:
def perform(data, count)
message = JSON.load(data)
UserMailer.send_invoice(message['invoice'], message['domain']).deliver
end
然后我收到一条错误消息 ActionView::Template::Error: undefined method '[]' for nil:NilClass
经过一番调查,我发现问题出在电子邮件中使用的变量上。当我在不使用变量的情况下尝试使用相同的电子邮件时,一切正常。
现在我明白这是一个关于 Sidekiq 如何在 Redis 中存储数据的问题(如 json)。我不知道的是,如何使用 Sidekiq 将变量传递给电子邮件模板?
编辑:这是我的 Mailer 和我的 Worker:
邮寄者:
def send_invoice(invoice, domain)
@invoice = Invoice.find(invoice)
@user = User.find(domain)
attachments["invoice.pdf"] = File.read("#{Rails.root}/invoices/pdf/55f5c596019e2b51af000000-55f82b520540a78f43000000.pdf")
mail to: "email@example.com", subject: "Invoice", reply_to: @user.email, :from => @user.email
end
工人:
def perform(data, count)
message = JSON.load(data)
UserMailer.send_invoice(message['invoice'], message['domain']).deliver
end
在我的控制器中:
...
h = JSON.generate({ 'invoice' => @invoice.id.to_s, 'domain' => set_db })
PostmanWorker.perform_async(h, 1)
...
因此,如您所见,我正在传递要在工作程序内部处理的 id,而不是传递哈希。但是如何将 @invoice
散列传递给电子邮件模板?
根据文档 (https://github.com/mperham/sidekiq/wiki/Active-Job),有一个更新的语法 (API) 用于发送电子邮件:
UserMailer.welcome_email(@user).deliver_later!(wait: 1.hour)
查看 "Action Mailer" 下的部分,因为这可能会解决您的问题。