views:

864

answers:

4

Hi guys, I'm running a strange problem sending emails. Basicly 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

A: 

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.

Dave Nolan
A: 

Thanks, I tried also your solution but I'm getting the same problem. Could it be a library / gem incompatibility problem? It seems that it is not able to create the record on the email database. I had exactly the same code in another application and there it works fine (same server).

Roberto
What have you set email_class to be? http://seattlerb.rubyforge.org/ar_mailer/classes/ActionMailer/ARMailer.html#M000002
Dave Nolan
+1  A: 

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
dan-manges
A: 

Thanks, that was my problem! Now it is fixed!

Roberto

Roberto