ruby-on-rails – 电子邮件中的Prawn pdf附件

在我的Rails应用程序中,我试图将发票附加到电子邮件中:
def invoice(invoice)
  attachment :content_disposition => "attachment",:body => InvoicePdf.new(invoice),:content_type => "application/pdf",:filename => 'invoice.pdf'

  mail(:to => @user.email,:subject => "Your Invoice")
end

InvoicePdf是一个对虾PDF文档:

class InvoicePdf < Prawn::Document
  def initialize(order,view)
    draw_pdf
  end

  def draw_pdf
    # pdf stuff
  end
end

电子邮件中没有附件.我究竟做错了什么?任何提示将受到欢迎和赞赏.

编辑:我使用的Rails版本是3.0.x

解决方法

看看 Action Mailer Guide.你需要调用附件方法给你添加一个附件.

尝试这个:

attachments['attachment_filename'] = InvoicePdf.new(invoice)

这是假设调用InvoicePdf.new(invoice)生成一个文件并返回一个表示该文件的IO对象.我也注意到,您的InvoicePdf类初始化程序期望两个参数,但您只传递一个参数.

更新:
还要注意的是,Action Mailer将取得文件名并设计出mime类型,设置Content-Type,Content-disposition,Content-transfer-encoding和base64编码附件内容全部为您手动设置必要的,除非你想覆盖认值.

基于您的pdf生成方法,这可能会更好:

invoice = InvoicePdf.new(invoice)
attachments["invoice.pdf"] = { :mime_type => 'application/pdf',:content => invoice.render }
mail(:to => @user.email,:subject => "Your Invoice")

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...