2

我在发送电子邮件时遇到了一个奇怪的问题。我得到了这个例外:

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 = "xxx@example.com"
    @from = "xxx@example.com"
    @subject = "TEST MAIL SUBJECT"
    @body = "<br>TEST MAIL MESSAGE"
    @content_type = "text/html"
  end
end

你有什么主意吗?

谢谢!罗伯托

4

2 回答 2

1

问题在于 ar_mailer 用于存储消息的模型。您可以在回溯中看到异常来自 ActiveRecord::Base.create 调用初始化时。通常,ActiveRecord 构造函数需要一个参数,但在这种情况下,您的模型似乎没有。ar_mailer 应该使用名为 Email 的模型。你的 app/models 目录中有这个类吗?如果是这样,初始化是否覆盖了任何内容?如果您要覆盖初始化,请务必为其提供参数并调用 super。

class Email < ActiveRecord::Base
  def initialize(attributes)
    super
    # whatever you want to do
  end
end
于 2008-09-26T07:09:25.807 回答
0

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

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

class TestMailer < ActionMailer::ARMailer
  def send_email
    recipients "roberto.druetto@gmail.com"
    from "roberto.druetto@gmail.com"
    subject "TEST MAIL SUBJECT"
    content_type "text/html"
  end
end

来自文档: body 方法具有特殊行为。它需要一个散列,该散列生成一个实例变量,该变量以散列中的每个键命名,其中包含该键指向的值。

所以像这样的东西添加到上面的方法中:

body :user => User.find(1)

将允许您@user在模板中使用。

于 2008-09-25T12:42:02.553 回答