【发布时间】:2019-01-04 16:06:15
【问题描述】:
我在后台作业中为发票生成 PDF 文件,我想将其附加到发票上。我使用 Carrierwave 进行文件上传,但在这里我不从 UI 上传它。我希望能够附加文件而不将其保存在磁盘上。
invoice.rb
mount_uploader :file, InvoiceFileUploader
后台工作
class GeneratePdfJob < ApplicationJob
queue_as :default
def perform(invoice)
pdf = InvoiceServices::PdfGenerator.new(invoice)
file_name = [invoice.number.gsub('/','-'), invoice.due_date.to_s, SecureRandom.urlsafe_base64].join('-') + '.pdf'
pdf.render_file(file_name)
file = File.new(file_name)
invoice.file = file
File.delete(file_name)
end
end
所以现在我调用render_file 方法来实际创建文件,但是这个文件保存在我的应用程序的根文件夹中,所以我需要在之后删除它。有没有更好的办法?有没有办法在不实际保存在磁盘上的情况下附加文件?
【问题讨论】:
标签: ruby-on-rails prawn