3

我正在尝试解析发送到我的应用服务器的电子邮件。它应该阅读电子邮件以通过电子邮件找到用户,然后将照片添加到用户的照片模型中 这是我到目前为止所拥有的。我究竟做错了什么?

class Mailman < ActionMailer::Base 
  def receive(email) 
    logger.info("Got an email about: #{email.subject}") 
    if (@user = User.find_by_email(email.from)) 
      if email.has_attachments? 
        for attachment in email.attachments 
        #...@user.photos.create(:data_file_name => attachment.original_filename, 
        # :data_content_type => attachment.content_type, :data_file_size => attachment.size, 
        # :data_updated_at => Time.now.to_datetime) 
        # @user.photos << attachment 
        # I don't think this is the right way to do this... 
        end 
      end 
    else 
      logger.info("No user found with email: #{email.from}") 
    end 
  end 
end 

class User < ActiveRecord::Base 
  acts_as_authentic 
  has_attached_file :avatar 
  has_many :photos, :dependent => :destroy 
  accepts_nested_attributes_for :photos 
  # What does this do? 
end 


class Photo < ActiveRecord::Base 
  belongs_to :user 
  has_attached_file :data 
end 


class AddAttachmentsDataToPhoto < ActiveRecord::Migration 
  def self.up 
    add_column :photos, :data_file_name, :string 
    add_column :photos, :data_content_type, :string 
    add_column :photos, :data_file_size, :integer 
    add_column :photos, :data_updated_at, :datetime 
  end 
  def self.down 
    remove_column :photos, :data_file_name 
    remove_column :photos, :data_content_type 
    remove_column :photos, :data_file_size 
    remove_column :photos, :data_updated_at 
  end 
end
4

2 回答 2

1

我知道这有点晚了,但是对于遇到这个问题的其他人,我写了一篇博客文章,解释了如何使用回形针在 Rails 3 中保存来自传入电子邮件的附件

于 2011-07-16T11:41:50.207 回答
0

你没有提到它,但看起来你正在使用 Paperclip。根据文档,您需要将文件分配给附件。在你的循环中,如果你这样做了怎么办:

@user.photos.create(:data => attachment)
于 2010-04-27T18:24:46.553 回答