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
在我的web_reservations_controller中,我有一个简单的方法调用
TestMailer.deliver_send_email
And my TesMailer is something like:
我的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
Do you have any idea?
你有什么主意吗?
Thanks! Roberto
2 个解决方案
#1
1
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.
问题在于ar_mailer用于存储消息的模型。您可以在回溯中看到异常来自ActiveRecord :: Base.create,当它调用initialize时。通常,ActiveRecord构造函数接受一个参数,但在这种情况下,它看起来像你的模型没有。 ar_mailer应该使用名为Email的模型。你的app / models目录中有这个类吗?如果是这样,是否有任何覆盖初始化?如果你要覆盖初始化,请务必给它参数并调用super。
class Email < ActiveRecord::Base
def initialize(attributes)
super
# whatever you want to do
end
end
#2
0
Check that email_class is set correctly: http://seattlerb.rubyforge.org/ar_mailer/classes/ActionMailer/ARMailer.html#M000002
检查email_class是否设置正确: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 "roberto.druetto@gmail.com"
from "roberto.druetto@gmail.com"
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.
来自docs:body方法有特殊的行为。它需要一个哈希,它生成一个实例变量,该变量以哈希中包含该键所指向的值的每个键命名。
So something like this added to the method above:
所以这样的东西添加到上面的方法:
body :user => User.find(1)
Will allow you to use @user
in the template.
允许您在模板中使用@user。
#1
1
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.
问题在于ar_mailer用于存储消息的模型。您可以在回溯中看到异常来自ActiveRecord :: Base.create,当它调用initialize时。通常,ActiveRecord构造函数接受一个参数,但在这种情况下,它看起来像你的模型没有。 ar_mailer应该使用名为Email的模型。你的app / models目录中有这个类吗?如果是这样,是否有任何覆盖初始化?如果你要覆盖初始化,请务必给它参数并调用super。
class Email < ActiveRecord::Base
def initialize(attributes)
super
# whatever you want to do
end
end
#2
0
Check that email_class is set correctly: http://seattlerb.rubyforge.org/ar_mailer/classes/ActionMailer/ARMailer.html#M000002
检查email_class是否设置正确: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 "roberto.druetto@gmail.com"
from "roberto.druetto@gmail.com"
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.
来自docs:body方法有特殊的行为。它需要一个哈希,它生成一个实例变量,该变量以哈希中包含该键所指向的值的每个键命名。
So something like this added to the method above:
所以这样的东西添加到上面的方法:
body :user => User.find(1)
Will allow you to use @user
in the template.
允许您在模板中使用@user。