2008-09-25 42 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