我是一个处理客户门票(worequests)的Rails应用程序。门票在客户和员工之间来回发表评论。评论可以有附件。它在Heroku上运行,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"=>"60@mail.myapp.com", "recipients"=>{"0"=>"60@mail.myapp.com"}, "from"=>"someguy@gmail.com", "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
但是,如何设置PaperClip :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,
感谢您的帮助!
答案 0 :(得分:2)
这有效:
:attach => params[:attachments]['0'],