2008-09-25 82 views
2

我运行一个奇怪的问题发送电子邮件。我得到这个异常:错误的参数错误与TestMailer

ArgumentError (wrong number of arguments (1 for 0)): 
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/base.rb:642:in `initialize' 
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/base.rb:642:in `new' 
/usr/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/base.rb:642:in `create' 
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.3.1/lib/action_mailer/ar_mailer.rb:92:in `perform_delivery_activerecord' 
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.3.1/lib/action_mailer/ar_mailer.rb:91:in `each' 
/usr/lib/ruby/gems/1.8/gems/ar_mailer-1.3.1/lib/action_mailer/ar_mailer.rb:91:in `perform_delivery_activerecord' 
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `__send__' 
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:508:in `deliver!' 
/usr/lib/ruby/gems/1.8/gems/actionmailer-2.1.1/lib/action_mailer/base.rb:383:in `method_missing' 
/app/controllers/web_reservations_controller.rb:29:in `test_email' 

在我web_reservations_controller我有一个简单的方法调用

TestMailer.deliver_send_email 

而且我TesMailer是一样的东西:

class TestMailer < ActionMailer::ARMailer 
    def send_email 
    @recipients = "[email protected]" 
    @from = "[email protected]" 
    @subject = "TEST MAIL SUBJECT" 
    @body = "<br>TEST MAIL MESSAGE" 
    @content_type = "text/html" 
    end 
end 

你有什么想法?

谢谢! 罗伯托

回答

1

问题出在ar_mailer用来存储消息的模型上。您可以在回溯中看到异常来自ActiveRecord :: Base.create,当它调用initialize时。通常一个ActiveRecord构造函数需要一个参数,但在这种情况下,它看起来像你的模型没有。 ar_mailer应该使用称为电子邮件的模型。你的应用/模型目录中是否有这个类?如果是这样,是否有任何重写初始化?如果你重写初始化,一定要给它参数并且调用super。

class Email < ActiveRecord::Base 
    def initialize(attributes) 
    super 
    # whatever you want to do 
    end 
end 
0

检查email_class设置正确:http://seattlerb.rubyforge.org/ar_mailer/classes/ActionMailer/ARMailer.html#M000002

也不要使用实例变量。尝试:

class TestMailer < ActionMailer::ARMailer 
    def send_email 
    recipients "[email protected]" 
    from "[email protected]" 
    subject "TEST MAIL SUBJECT" 
    content_type "text/html" 
    end 
end 

从文档:身体方法有特殊的行为。它需要一个哈希,它生成一个实例变量,该实例变量以包含该关键字指向的值的哈希中的每个关键字命名。

因此,像这样加入到上述方法:

body :user => User.find(1) 

将允许你在模板中使用@user