The following code is meant to aggregate a handful of PDF docs and send a zip file with all the PDF's contained. The code works, but only 50% of the time.
def donor_reports
@starting_date = params['date'].to_date
@ending_date = @starting_date.to_date.end_of_month
@categories = Category.all
zipfile_name = Tempfile.new(['records', '.zip'])
Zip::File.open(zipfile_name.path, Zip::File::CREATE) do |zipfile|
@categories.each do |category|
temp_pdf = Tempfile.new(['record', '.pdf'])
temp_pdf.binmode
temp_prawn_pdf = CategoryReport.new
temp_prawn_pdf.generate_report(category, @starting_date, @ending_date)
temp_pdf.write temp_prawn_pdf.render
temp_pdf.rewind
zipfile.add("#{category.name} - Donation Report.pdf", "#{temp_pdf.path}")
end
end
send_file zipfile_name
end
The other 50% of the time it throws the following error -
No such file or directory @ rb_sysopen - /var/folders/jy/w7tv9n8n7tqgtgmv49qz7zqh0000gn/T/record20160114-10768-1guyoar.pdf
Any advice/guidance here would be much appreciated! First time using Tempfile and Rubyzip.