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

0

あなたはそれについて言及していませんでしたが、Paperclip を使用しているようです。docs によると、ファイルを添付ファイルに割り当てる必要があります。あなたのループで、これをした場合はどうなりますか:

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