我需要将有关向我的客户发送每封电子邮件的信息保存在数据库中以供进一步分析。所以我试图在观察者中做到这一点,但我需要有关发票的信息。 所以我得到了 Mailer:
class ClientMailer < ActionMailer::Base
default :from => "no-reply@tori-app.herokuapp.com"
def remind(client, invoices)
@client = client
@company = @client.company
@invoices = invoices.to_a
@template = t('message.template')
@text = liquid_parse @template
@html = markdown_parse @text
mail(:to => @client.email, :subject => t('message.title')) do |format|
format.html
format.text
end
end
private
def markdown_parse(text)
markdown = Redcarpet::Markdown.new Redcarpet::Render::HTML,
:autolink => true, :space_after_headers => true
markdown.render text
end
def liquid_parse(text)
renderer = Liquid::Template.parse text
renderer.render 'company' => @company, 'invoice' => @invoice, 'client' => @client
end
end
问题是:如何将 @invoices
传递给 ActionMailer 观察者?
最佳答案
我是通过添加邮件自己完成的:
headers 'X-Invoice-IDs' => @invoices.map(&:id).join(';')
然后在观察者中
ids = message.header['X-Invoice-IDs'].to_s.split ';'
我的观察者中已经有了所有的发票 ID。
编辑
在 PRUG 的星期四 session 之后我想到了重载 delivery
方法而不是使用观察者的想法。它将与 Rails 4 兼容并且更易于使用(因为我不需要传递额外的 header ,我将只使用实例变量)。
关于ruby-on-rails - 将附加信息传递给 ActionMailer Observer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9455656/