2010-11-24 11 views
1

からペーパークリップやCarrierwaveてファイルをアップロードする:は例えば、メールの添付ファイル

mail = Mail.new do 
    from  "[email protected]" 
    to  "[email protected]" 
    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/Paperclipに添付ファイルを渡します(これについては、私はどちらを使っても問題ありません)。私はいくつかの方法を試しましたが、さまざまな障害にぶち当たっています。

私の現在の試みは次のようになります。これは表示されません

mail.attachments.each do |attachment| 
    self.attachments << Attachment.new(:file => Tempfile.new(attachment.filename) {|f| f.write(attachment.decoded)}) 
end 

は仕事に - 任意のヒント? end

答えて

6

私はメール添付ファイルを取ってクリップでそれを使用しようとしたとき、私はいくつかの問題に遭遇しました。私が覚えている問題は、paperclipが渡された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) 
+0

素晴らしい、完璧に働いています。以前はStringIOオブジェクトを使用しようとしましたが、余分な属性に落ちました。attr_accessorsとして設定することは考えられませんでした。どうもありがとうございました :) – robotmay

関連する問題