rake-write-code
rake-write-code

Reputation: 43

Missing template when attaching pdf to actionmailer and wicked pdf

I get Missing template when trying to send an email with a pdf attachment:

Controller

   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

Mailer

    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

The show.pdf.erb template is there already.

Upvotes: 0

Views: 787

Answers (1)

ReggieB
ReggieB

Reputation: 8212

I think you need to change the render_to_string line to either:

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

or

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

At the moment, I think the system is looking for app/views/bike_services/show.pdf.erb in /Users/vangama/projects/mm-crm/app/views which is effectively /Users/vangama/projects/mm-crm/app/views/app/views/bike_services/show.pdf.erb which doesn't exist.

Upvotes: 2

Related Questions