Ruby Gem:Axlslx RuntimeError:调用`mail`后无法添加附件

时间:2019-03-07 21:52:31

标签: ruby-on-rails ruby axlsx

我要遍历我的所有shop记录,如果order错误,则向他们发送电子邮件。它可以正常工作到2或 3家商店,但此后出现此错误:

RuntimeError: Can't add attachments after邮件was called. Make sure to use attachments[]= before calling mail.

这是我用于创建和发送附件的代码:

 def order_report
    shops = Shop.where(subscribed: true, setup: true)
    shops.each do |shop|
      @sheet = OrderReport.new(shop)
      @sheet.create_order_workbook
      get_data_for_inside_email
      time = Time.now
      attachments["test_book.xlsx"] = File.read("test_book.xlsx")
      excel_sheet_line_count = %x{wc -l "test_book.xlsx"}.split[0].to_i #this is just seeing how many lines the file is.. an empty sheet returns as 13
      if excel_sheet_line_count > 13
        mail(to: "some_email@gmail.com", subject: "FBA Shipping Report for #{time.strftime("%B %d")}")
      end
    end
  end

1 个答案:

答案 0 :(得分:0)

如果有人遇到此问题,我将在此处添加。我永远无法使上面的代码正常工作,但是我发现了这一点,它的工作原理就像一个魅力:

mail(to: "todd@gmail.com", 
        subject: "FBA Shipping Report for #{time.strftime("%B %d")}").attachments["problem_orders_#{shop.id}.xlsx"] = File.read("problem_orders_#{shop.id}.xlsx")