Вопрос

I'm generating an invoice pdf via wicket_pdf and this works. This pdf will be used as attachment in an email in a later step of process.

# 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

When I call the RAKE TASK the pdf is empty. I debugged it already and the file name exists and the original pdf file is ok, but only the attached pdf inside the email is empty.

I use letter_opener (1.0.0) in development mode.

Does anybody know, why there is a difference between console and executing the same code via rake task? AND how could I debug this difference?

Это было полезно?

Решение

found the problem. this was really strange. i found solution while comparing the two pdfs in a hex-editor.

in rake i had to set ruby standard encoding to utf-8

# users.rake 
namespace :users do
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
  ...

i hope that maybe others could profit from my 2 days effort :-). cheers

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top