我目前有一个使用wicked_pdf和carrierwave功能的rails app(4.2.1)。用户当前单击将家庭评估视图呈现为PDF的按钮,然后用户将PDF下载到他们的计算机并导航到评估表单,然后他们提交PDF并将其成功上载到AWS。
这一切都很好,但我想做的是“简化”这个过程。当用户单击按钮时,将创建PDF并将其传递给carrierwave,以使用carrierwave / fog上传到AWS。我现在有一个视图链接到控制器,该控制器调用FamilyAssesment.upload_assessment方法。
我一直在尝试不同的方法,但似乎无法准确理解我需要做些什么才能让它发挥作用。
视图上的按钮
<%= link_to "Upload", family_assessment_pdf_upload_url(id: "#{f.object.case.id}"), class: 'button radius default tiny pdf-button' %>
family_assessments_controller.rb
class FamilyAssessmentsController < ApplicationController
def family_assessment_pdf_upload
c = Case.find(params[:id])
family_assessment = c.family_assessment
assessment = c.assessments.where(title: 'Families First Assessment').first
FamilyAssessment.upload_assessment(assessment, family_assessment)
#If work is done and url saved to assessment.assessment do some redirect action here
end
end
family_assessment.rb
class FamilyAssessment < ActiveRecord::Base
def self.upload_assessment(assessment, family_assessment)
family_assessment = family_assessment
assessment = assessment
#I think this is how I would create the PDF in preparation for hand off to carrierwave
pdf = WickedPdf.new.pdf_from_string(
render_to_string(:pdf => "family_assessment",:template => 'family_assessments/family_assessment.pdf.erb', :locals => {:family_assessment => family_assessment})
)
#Pass pdf to carrierwave and save url in assessment.assessment
end
end
答案 0 :(得分:1)
看起来您可以将PDF保存到临时文件,然后上传该临时文件。
class FamilyAssessment < ActiveRecord::Base
def self.upload_assessment(assessment, family_assessment)
family_assessment = family_assessment
assessment = assessment
#I think this is how I would create the PDF in preparation for hand off to carrierwave
pdf = WickedPdf.new.pdf_from_string(
render_to_string(:pdf => "family_assessment",:template => 'family_assessments/family_assessment.pdf.erb', :locals => {:family_assessment => family_assessment})
)
#Pass pdf to carrierwave and save url in assessment.assessment
# Write it to tempfile
tempfile = Tempfile.new("#{Rails.root}/tmp/assessment_#{self.id}.pdf")
tempfile.binmode
tempfile.write pdf_file
tempfile.close
# Attach that tempfile to the invoice
unless pdf_file.blank?
self.uploads.clear
self.uploads.create(fileinfo: File.open(tempfile.path), job_id: self.job.id)
tempfile.unlink
end
end
end
这个答案有很多来自Getting PDF from WickedPDF for attachment via Carrierwave