2014-01-14 54 views
1

我是一個處理客戶入場券(worequests)的Rails應用程序。門票在客戶和員工之間有來回的評論。評論可以有附件。它在Heroku上運行,PaperClip在S3中存儲附件。Rails使用CloudMailIn和Paperclip將附件保存到S3

創建新評論並將電子郵件發送給分配給該工單的客戶和員工。

我正在使用CloudMailIn,以便客戶或員工可以用新評論回覆評論電子郵件。

到目前爲止,工作正常!

但是,我想允許返回的電子郵件包含一個附件。

這是工作接收郵件控制器:

class IncomingMailsController < ApplicationController 
    skip_before_filter :verify_authenticity_token 

    def create 
    Rails.logger.info params 
    worequest = params[:envelope][:to].split('@')[0] 
    contents = params[:plain].split('---')[0] 
    message = Comment.new(
     :worequest_id => worequest, 
     :user_id => User.find_by_email(params[:envelope][:from]).id, 
     :comments => contents, 
     :tenant_id => 1 
    ) 
    if message.save 
     render :text => 'Success', :status => 200 
    else 
     render :text => message.errors.full_messages, :status => 422, :content_type => Mime::TEXT.to_s 
    end 
end 

Rails.logger.info params日誌結果包括這樣的:

"envelope"=>{"to"=>"[email protected]", "recipients"=>{"0"=>"[email protected]"}, "from"=>"[email protected]", "helo_domain"=>"mail-wi0-f175.google.com", "remote_ip"=>"xxx.xx.xxx.xxx", "spf"=>{"result"=>"temp_error", "domain"=>"mydomain.com"}}, 

"attachments"=>{"0"=>#<ActionDispatch::Http::UploadedFile:0x00000007194040 @original_filename="five guys.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"attachments[0]\"; filename=\"five guys.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20140115-5-1n2rb24>>}, "action"=>"create", "controller"=>"incoming_mails"} 

附件是哈希和「0」是關鍵的名稱。 params[:attachments][‘0’]

我可以訪問這些字段罰款:

Rails.logger.info params[:envelope][:from] 
Rails.logger.info params[:envelope][:to] 
Rails.logger.info params[:attachments]['0'].original_filename 
Rails.logger.info params[:attachments]['0'].content_type 

但是,我怎麼設置回形針:attach文件?

:attach => params[:attachments]['0'].tempfile ? 
:attach => params[:attachments]['0'].read ? 

這是我目前的嘗試:

attach = Attachment.new(
    :comment_id => 346, 
    :name => "Email Attachment", 
    :attach_file_name => params[:attachments]['0'].original_filename, 
    :attach_content_type => params[:attachments]['0'].content_type, 
    :attach => params[:attachments]['0'].path, 
    :tenant_id => 1 
) 

:attach => params[:attachments]['0'].path,是錯誤的。

聞聽此事:

Paperclip::AdapterRegistry::NoHandlerError (No handler found for "/tmp/RackMultipart20140115-13-tcpvtw"): 

我不知道該用什麼?????

:attach => params[:attachments]['0'].read, 
:attach => params[:attachments]['0'].path.to_file, 
:attach => params[:attachments]['0'].path.read, 

感謝您的幫助!

回答

2

這工作:

 :attach => params[:attachments]['0'], 
+0

我有一個類似的問題,在官方的例子將其顯示爲PARAMS [:附件] [0]而不是[0] ...一旦我改變了這一切,我可以直接分配東西,比如user.avatar = params [:attachments] [「0」] –

相關問題