将pdf附加到actionmailer和wicked pdf时缺少模板

问题描述 投票:0回答:1

[尝试发送带有pdf附件的电子邮件时,我得到了丢失的模板:

控制器

   respond_to do |format|
      format.html
      format.pdf do 
        render pdf: "job card ##{@bike_service.id} - #{DateTime.now}", :template => 'bike_services/show.html.erb' # Excluding ".pdf" extension.
      end
    end

    BikeServiceMailer.job_card_pdf_email(BikeService.last.id).deliver_now
  end

邮件程序

    def job_card_pdf_email(bike_service_id)
        bike_service = BikeService.find(bike_service_id)
        attachments["bike_service_#{bike_service.id}-#{DateTime.now}.pdf"] = WickedPdf.new.pdf_from_string(
        render_to_string(pdf: 'bike_service', template: File.join('app', 'views', 'bike_services', 'show.pdf.erb'), layout: 'pdf.html')
        )
        mail(to: todo.owner.email, subject: 'Your job card PDF is attached', bike_service: bike_service)
    end

show.pdf.erb模板已经存在。

ruby-on-rails actionmailer wicked-pdf
1个回答
1
投票

我认为您需要将render_to_string行更改为:

render_to_string(pdf: 'bike_service', template: File.join('bike_services', 'show.pdf.erb'), layout: 'pdf.html')

render_to_string(pdf: 'bike_service', template: Rails.root.join('app', 'views', 'bike_services', 'show.pdf.erb'), layout: 'pdf.html')

[目前,我认为系统正在app/views/bike_services/show.pdf.erb中寻找/Users/vangama/projects/mm-crm/app/views,实际上是不存在的/Users/vangama/projects/mm-crm/app/views/app/views/bike_services/show.pdf.erb

© www.soinside.com 2019 - 2024. All rights reserved.