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 + paperclip + 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 に答える