Rails 3 - Paperclip - 电子邮件附件

时间:2013-01-17 16:41:14

标签: ruby-on-rails-3 paperclip actionmailer

尝试在Rails 3中通过电子邮件发送Paperclip附件。

发票模型:

class Invoice < ActiveRecord::Base

has_attached_file :attachment,
:url => "http://server/app/attachments/:id/:style/:basename.:extension",
:path => ":rails_root/public/attachments/:id/:style/:basename.:extension"
validates_attachment_presence :attachment
validates_attachment_size :attachment, :less_than => 5.megabytes

end

account_mailer:

  def email_approver(invoice)
    @subject          = 'Invoice Approval Request'
    @body["invoice"]  = invoice
    #attachment  "application/octet-stream" do |a|
          #a.body = File.read(invoice.attachment.to_file.path)
          #a.filename = invoice.attachment_file_name
      #end
    @recipients = [invoice.approver_email, invoice.alternate_approver_email].compact
    @from             = "ADMIN"
    @sent_on          = Time.now
    @headers          = {}    
 end

错误:

NoMethodError (undefined method `filename=' for #<Mail::Part:0x00000002566dd0>):

有什么想法吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

你尝试过这样的事吗

attachments[invoice.attachment_file_name] = File.read(invoice.attachment.to_file.path)

答案 1 :(得分:0)

这是一个老问题,但这是我的工作解决方案:

Automata