2009-11-26 61 views
1

這裏是我的代碼:MMS2R和多張圖片的Rails

require 'mms2r' 

class IncomingMailHandler < ActionMailer::Base 

    ## 
    # Receives email(s) from MMS-Email or regular email and 
    # uploads that content the user's photos. 
    # TODO: Use beanstalkd for background queueing and processing. 
    def receive(email)  
    begin 
     mms = MMS2R::Media.new(email) 

     ## 
     # Ok to find user by email as long as activate upon registration. 
     # Remember to make UI option that users can opt out of registration 
     # and either not send emails or send them to a [email protected] 
     # type address. 
     ## 
     # Remember to get SpamAssasin 
     if (@user = User.find_by_email(email.from) && email.has_attachments?) 
     mms.media.each do |key, value| 
      if key.include?('image') 
      value.each do |file| 
       @user.photos.push Photo.create!(:uploaded_data => File.open(file), :title => email.subject.empty? ? "Untitled" : email.subject) 
      end 
      end 
     end 
     end 

    ensure 

     mms.purge 

    end 

    end 

end 

,這裏是我的錯誤:

/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/commands/runner.rb:48: undefined method `photos' for true:TrueClass (NoMethodError) 
    from /usr/home/xxx/app/models/incoming_mail_handler.rb:23:in `each' 
    from /usr/home/xxx/app/models/incoming_mail_handler.rb:23:in `receive' 
    from /usr/home/xxx/app/models/incoming_mail_handler.rb:21:in `each' 
    from /usr/home/xxx/app/models/incoming_mail_handler.rb:21:in `receive' 
    from /usr/local/lib/ruby/gems/1.8/gems/actionmailer-2.3.4/lib/action_mailer/base.rb:419:in `receive' 
    from (eval):1 
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `eval' 
    from /usr/local/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/commands/runner.rb:48 
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' 
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' 
    from /home/xxx/script/runner:3 

我發了電子郵件服務器有兩個圖像附件。在收到服務器運行的電子郵件後 「| ruby​​/xxx/script/runner'IncomingMailHandler.receive STDIN.read'」

發生了什麼事?我究竟做錯了什麼?

MMS2R docs

回答

1

請更換

if (@user = User.find_by_email(email.from) && email.has_attachments?) 

if ((@user = User.find_by_email(email.from)) && email.has_attachments?)