2

使用:Rails 4.2、Prawn、Paperclip、通过 ActiveJobs 的 DelayedJobs、Heroku。

我有一个非常大的 PDF,需要在后台处理。在我想要创建的自定义作业中,将其上传到 S3,然后在准备好后通过电子邮件向用户发送 url。我通过 PdfUpload 模型促进了这一点。

我的方法/代码有什么问题吗?我使用 File.open() 如我发现的示例中所述,但这似乎是我错误的根源( TypeError: no implicit conversion of FlightsWithGradesReport into String )。

  class PdfUpload < ActiveRecord::Base
    has_attached_file :report,
      path: "schools/:school/pdf_reports/:id_:style.:extension"
  end

/pages_controller.rb

  def flights_with_grades_report
    flash[:success] = "The report you requested is being generated.  An email will be sent to '#{ current_user.email }' when it is ready."
    GenerateFlightsWithGradesReportJob.perform_later(current_user.id, @rating.id)
    redirect_to :back
    authorize @rating, :reports?
  end

/ 工作

class GenerateFlightsWithGradesReportJob < ActiveJob::Base
  queue_as :generate_pdf

  def perform(recipient_user_id, rating_id)
    rating = Rating.find(rating_id)
    pdf = FlightsWithGradesReport.new( rating.id )
    pdf_upload = PdfUpload.new
    pdf_upload.report = File.open( pdf )
    pdf_upload.report_processing = true
    pdf_upload.report_file_name = "report.pdf"
    pdf_upload.report_content_type = "application/pdf"
    pdf_upload.save!
    PdfMailer.pdf_ready(recipient_user_id, pdf_upload.id)
  end
end

这会导致错误:

 TypeError: no implicit conversion of FlightsWithGradesReport into String
4

1 回答 1

1

改变这个:

pdf_upload.report = File.open( pdf )

对此:

pdf_upload.report = StringIO.new(pdf.render)

解决了我的问题。

于 2015-04-13T02:15:37.627 回答