我正在通过wicket_pdf生成发票pdf,这是有效的。此pdf文件将在稍后的处理步骤中作为电子邮件的附件使用。
# user.rb
def send_last_invoice_email!(invoice)
UserMailer.send_actual_invoice_email(self, invoice).deliver
end
# user_mailer.rb
def send_actual_invoice_email(user, invoice)
@user = user
@invoice = invoice
to = @user.email
# THIS IS THE PROBLEMATIC LINE
attachments["#{@invoice.file_name}"] = File.read(@invoice.file_name_path)
mail(:to => to)
end
# console => works
user = User.where("step_id = 1 AND status = 0").last
invoice = user.invoices.last
user.send_last_invoice_email!(invoice)
# rake task => doesn't work
desc "send email for first user for step_id => 1 testing purpose"
task :send_mail => :environment do
user = User.where("step_id = 1 AND status = 0").last
invoice = user.invoices.last
user.send_last_invoice_email!(invoice)
end
当我调用RAKE TASK时,pdf是空的。我已经调试过了,文件名是存在的,原来的pdf文件是可以的,但是只有邮件里面的附件pdf是空的。
我在开发模式下使用letter_opener (1.0.0)
有没有人知道,为什么控制台和通过rake任务执行相同的代码之间有区别?我该如何调试这个差异?
发现问题。这真的很奇怪。在十六进制编辑器中比较两个pdf文件时,我找到了解决方案。
在rake中,我必须将ruby标准编码设置为utf-8# users.rake
namespace :users do
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
...
我希望也许其他人可以从我2天的努力中获益:-)。欢呼声