【问题标题】:unable send pdf attachment in email generated from wicked-pdf in rails?无法在 Rails 中的 wicked-pdf 生成的电子邮件中发送 pdf 附件?
【发布时间】:2016-12-16 23:32:44
【问题描述】:

我正在尝试在电子邮件中将pdf 作为attachment 发送。但出现错误。

ctionView::MissingTemplate at /invoises/BRUqWOeEVNSN6GCwxQqLGg%253D%253D/send_invoice_email Missing template invoises/#{invoise.id}/show_pdf_invoice.pdf.erb with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :jpeg, :gif, :bmp, :tiff, :m

我正在使用wicked_pdfgem.trying 从html view 生成pdf

controller中的方法

  def send_invoice_email
    CompanyMailer.invoice(@invoise.id).deliver
    redirect_to invoices_path, notice: 'Invoice Sent successfully.' 
  end

mailer 中的逻辑:

def invoice(invoice)
  invoice = Invoice.find(invoice)
  attachments["invoice.pdf"] = WickedPdf.new.pdf_from_string(
    render_to_string(:pdf => "invoice",:template => 'invoices/#{invoice.id}/show_pdf_invoice.pdf.erb')
  )

  mail(to: "abc@gmail.com", subject: 'Your todo PDF is attached')
end

controller中的方法

  def show_pdf_invoice
    respond_to do |format|
      format.html { render :layout => false }
      format.pdf do
    render pdf: "show_pdf_invoice"
    #render :pdf => "pdf"#, :layout => 'pdf.html.erb'
      end
    end
  end

invoices/show_pdf_invoice.pdf.erb

  <h1>this is pdf invoice.<h1>

company_mailer/invoice.html.erb

  please find attached pdf.

【问题讨论】:

  • 不太熟悉render_to_string 的工作原理,但看起来您的字符串插值设置不正确。更改为"invoices/#{invoice.id}/show_pdf_invoice.pdf.erb" 使用双引号作为字符串插值的最外层引号。
  • 我改变了那行。还是一样的错误。
  • 更改为render_to_string(template: 'invoices/show_pdf_invoice.pdf.erb')

标签: ruby-on-rails email-attachments wicked-pdf


【解决方案1】:

问题已解决。我不需要在 url 中传递 id。我只需要通过render_to_string(template: 'invoices/show_pdf_invoice.pdf.erb')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    相关资源
    最近更新 更多