0

我正在尝试使用回形针将发送到我的 rails 应用程序的图像附件保存到 s3,但是我遇到了这些问题:

它进入附件循环并失败

NoMethodError (undefined method `size' for #<Mail::Part: 
0x2b62856e8030>): 
  app/mailers/user_mailer.rb:23:in `receive' 
  app/mailers/user_mailer.rb:14:in `each' 
  app/mailers/user_mailer.rb:14:in `receive' 
  app/controllers/emails_controller.rb:10:in `create' 

这是线

:photo_file_size => attachment.size, 

注释掉那一行,我在尝试创建时遇到了这个错误:

NoMethodError (undefined method `to_tempfile' for #<Mail::Part: 
    0x2ac5eb944220>): 

下面是我的代码。感谢帮助..

class UserMailer < ActionMailer::Base 
  def receive(email) 
     @user = User.find_or_create_by_email( 
                                    #:name => FIXME, 
                                    :email => email.from, 
                                    :password => 'password', 
                                    :password_confirmation => 'password' 
                                    ) 
     @item = Item.create(:title => email.subject, :user => @user, :price => 50) 
     if email.has_attachments? 
        for attachment in email.attachments 
        @item.photos.create( 
                            :photo => attachment, 
                            :photo_file_name => attachment.original_filename, 
                            :photo_content_type => attachment.content_type, 
                            :photo_file_size => attachment.size, 
                            :photo_updated_at => Time.now.to_datetime) 
         @item.photos << attachment 
        end 
      end 
   end 
end 

对我的附件对象进行检查给了我这个:

#<Mail::Part:23597877753640, Multipart: false, Headers: <Date: Wed, 25 Aug 2010 16:55:07 -0700>, <Mime-Version: 1.0>, <Content-Type: image/JPG; name="photo.jpeg">, <Content-Transfer-Encoding: base64>, <Content-Disposition: inline; filename=photo.jpeg>, <Content-ID: <4c75ad5b3cbed_52fe15764b0bf938695a@railgun64.30856.mail>>>
4

2 回答 2

1

我在使用 rails 3.0.0 + 回形针 + ActionMailer 时遇到了同样的问题。我能够通过以下方式解决这个问题(丑陋但有效):

if mail_message.has_attachments? 
  for attachment in mail_message.attachments 
    tempfile=File.open(attachment.original_filename,'w')
    tempfile.write_nonblock(attachment.body)
    asset = Asset.new(:photo => File.open(tempfile))
    asset.save!
    tempfile.close
  end  
end
于 2010-09-11T21:57:29.317 回答
0

我在http://minimul.com/full-proof-attachment-size-in-rails-3.html上写了一篇简短的博客文章

简而言之,这样做

photo_file_size => defined?(attachment.decoded) ? attachment.decoded.length : attachment.size,
于 2010-12-16T13:40:16.343 回答