Ruby on rails 通过回形针或Carrierwave从邮件附件上载文件

Ruby on rails 通过回形针或Carrierwave从邮件附件上载文件,ruby-on-rails,ruby,email,file-upload,Ruby On Rails,Ruby,Email,File Upload,如果我有邮件对象,例如: mail = Mail.new do from "jim@gmail.com" to "jane@yahoo.com" subject "Example" text_part do body "Blarg" end add_file "/some/file/or/some_such.jpg" end 如果我在申请中收到上述邮件 received_mail = mail.encoded Message.

如果我有邮件对象,例如:

mail = Mail.new do
  from      "jim@gmail.com"
  to        "jane@yahoo.com"
  subject   "Example"
  text_part do
    body    "Blarg"
  end
  add_file  "/some/file/or/some_such.jpg"
end
如果我在申请中收到上述邮件

received_mail = mail.encoded
Message.parse(received_mail)
我如何将附件传递到CarrierWave/回形针上?我将使用哪一个处理得最好?我尝试过几种不同的方法,但我总是遇到各种各样的障碍——有人能找到有效的解决方法吗

我目前的尝试是:

mail.attachments.each do |attachment|
  self.attachments << Attachment.new(:file => Tempfile.new(attachment.filename) {|f| f.write(attachment.decoded)})
end
这似乎不起作用-有什么提示吗?
结束

我知道,当我试图将邮件附件与回形针一起使用时,我也遇到了一些问题。我记得的问题是,回形针希望传递给它的文件对象具有某些属性

我是这样解决的:

mail.attachments.each do |attachment|
  file = StringIO.new(attachment.decoded)
  file.class.class_eval { attr_accessor :original_filename, :content_type }
  file.original_filename = attachment.filename
  file.content_type = attachment.mime_type

  #Then you attach it where you want it
  self.attachments << Attachment.new(:file => file)

我知道,当我试图将邮件附件与回形针一起使用时,我也遇到了一些问题。我记得的问题是,回形针希望传递给它的文件对象具有某些属性

我是这样解决的:

mail.attachments.each do |attachment|
  file = StringIO.new(attachment.decoded)
  file.class.class_eval { attr_accessor :original_filename, :content_type }
  file.original_filename = attachment.filename
  file.content_type = attachment.mime_type

  #Then you attach it where you want it
  self.attachments << Attachment.new(:file => file)

太棒了,效果很好。我以前尝试过使用StringIO对象,但我对额外的属性感到失望——从未想过将它们设置为attr_访问器。非常感谢:太棒了,效果非常好。我以前尝试过使用StringIO对象,但我对额外的属性感到失望——从未想过将它们设置为attr_访问器。非常感谢你: