用户邮件程序失败

时间:2010-02-02 14:48:35

标签: ruby-on-rails email

我在我的用户模型中设置了一个进程,将一堆@users发送到邮件脚本user_mailer.rb

我正在使用http://postageapp.com应用发送电子邮件。用户正在访问User_mailer但我从那里收到错误。任何人都可以指出我正确的方向。

用户模型:

class User < ActiveRecord::Base

  acts_as_authentic

  def self.mail_out

    weekday = Date.today.strftime('%A').downcase

    @users = find(:all, :conditions => {"#{weekday}sub".to_sym => 't'})




    UserMailer.deliver_mail_out(@users)



  end

end

User_mailer.rb

class UserMailer < ActionMailer::Base
    def mail_out(users)
    @recipients = { }
    users.each do |user|
      @recipients[user.email] = { :zipcode => user.zipcode }
    end


    from        "no-reply@dailytrailer.net"
    subject     "Check out the trailer of the day!"
    body        :user => user
  end

end

mail_out.html.erb

{{zipcode}},

Please check out the trailer of the day at http://www.dailytrailer.net


Thank you!
--
The DailyTrailer.net Team

用户数据库架构

  create_table "users", :force => true do |t|
    t.string   "email"
    t.date     "birthday"
    t.string   "gender"
    t.string   "zipcode"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "crypted_password"
    t.string   "password_salt"
    t.string   "persistence_token"
    t.string   "mondaysub",         :default => "f", :null => false
    t.string   "tuesdaysub",        :default => "f", :null => false
    t.string   "wednesdaysub",      :default => "f", :null => false
    t.string   "thursdaysub",       :default => "f", :null => false
    t.string   "fridaysub",         :default => "f", :null => false
    t.string   "saturdaysub",       :default => "f", :null => false
    t.string   "sundaysub",         :default => "f", :null => false
  end

错误:

/var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:48: undefined method `name' for #<User:0xb6e8ae48> (NoMethodError)
    from /home/tnederlof/Dropbox/Ruby/daily_trailer/app/models/user_mailer.rb:5:in `mail_out'
    from /home/tnederlof/Dropbox/Ruby/daily_trailer/app/models/user_mailer.rb:4:in `each'
    from /home/tnederlof/Dropbox/Ruby/daily_trailer/app/models/user_mailer.rb:4:in `mail_out'
    from /home/tnederlof/.gem/ruby/1.8/gems/actionmailer-2.3.5/lib/action_mailer/base.rb:459:in `__send__'
    from /home/tnederlof/.gem/ruby/1.8/gems/actionmailer-2.3.5/lib/action_mailer/base.rb:459:in `create!'
    from /home/tnederlof/.gem/ruby/1.8/gems/actionmailer-2.3.5/lib/action_mailer/base.rb:452:in `initialize'
    from /home/tnederlof/.gem/ruby/1.8/gems/actionmailer-2.3.5/lib/action_mailer/base.rb:395:in `new'
    from /home/tnederlof/.gem/ruby/1.8/gems/actionmailer-2.3.5/lib/action_mailer/base.rb:395:in `method_missing'
    from /home/tnederlof/Dropbox/Ruby/daily_trailer/app/models/user.rb:13:in `mail_out'
    from (eval):1
    from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `eval'
    from /var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:48
    from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from script/runner:3

2 个答案:

答案 0 :(得分:2)

我不确定这是否是完整的解决方案(你得到了什么错误?),但这是需要修复的:在body :user => user行,user变量不是定义。你的意思是:user => @recipients吗?

答案 1 :(得分:0)

我在你的代码中注意到的一件事是你的邮件程序类不是从Postage :: Mailer派生的。

首先确保安装了最新的PostageApp插件,然后在这里查看一些示例代码:http://postageapp.com/docs/rails

此处还有一个示例应用:http://blog.postageapp.com/2009/11/rails-example-app/

我在你的代码中注意到的另一个细节是在行中 body :user => user 不需要,因为你没有在你的视图中使用user变量,除了像Alex提到的那样,它在那时没有被定义。

希望有所帮助!