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>>>