TestMailer 的参数数量错误

发布于 2024-07-06 06:47:57 字数 1729 浏览 5 评论 0原文

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

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

你有什么想法吗?

谢谢! 罗伯托

I'm running a strange problem sending emails. I'm getting this exception:

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'

In my web_reservations_controller I have a simply method calling

TestMailer.deliver_send_email

And my TesMailer is something like:

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

Do you have any idea?

Thanks!
Roberto

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吐个泡泡 2024-07-13 06:47:57

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

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

The problem is with the model that ar_mailer is using to store the message. You can see in the backtrace that the exception is coming from ActiveRecord::Base.create when it calls initialize. Normally an ActiveRecord constructor takes an argument, but in this case it looks like your model doesn't. ar_mailer should be using a model called Email. Do you have this class in your app/models directory? If so, is anything overridden with initialize? If you are overriding initialize, be sure to give it arguments and call super.

class Email < ActiveRecord::Base
  def initialize(attributes)
    super
    # whatever you want to do
  end
end
微凉徒眸意 2024-07-13 06:47:57

检查 email_class 设置是否正确: http://seattlerb.rubyforge.org /ar_mailer/classes/ActionMailer/ARMMailer.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 方法有特殊的行为。 它采用一个散列,该散列生成一个以散列中的每个键命名的实例变量,其中包含该键所指向的值。

因此,在上面的方法中添加了这样的内容:

body :user => User.find(1)

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

Check that email_class is set correctly: http://seattlerb.rubyforge.org/ar_mailer/classes/ActionMailer/ARMailer.html#M000002

Also don't use instance variables. Try:

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

From the docs: the body method has special behavior. It takes a hash which generates an instance variable named after each key in the hash containing the value that that key points to.

So something like this added to the method above:

body :user => User.find(1)

Will allow you to use @user in the template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文