martincarlin87
martincarlin87

Reputation: 11062

Sending Simple Email in Rails

I've read a few questions and http://guides.rubyonrails.org/action_mailer_basics.html on how to send emails with Rails but can't seem to get it to work within the context of my current application.

I had an existing emailer.rb with a couple of methods that were identical apart from the parameters they accepted were named differently so I copied their format:

def quotation_notification(q)
  @recipients  = q.recipient_email
  @from        = q.partner_name + "<#{q.partner_email}>"
  @subject     = "New Quotation from " + q.partner_name
  @body[:q]    = q
end

I then created a new view file in emailers named quotation_notification.rhtml which just contains text for the moment.

I am then calling the function from inside a different controller and sending hardcoded parameters for now:

q = QuotationEmail.new(:recipient_email => '[email protected]', :partner_name => 'Martin Carlin', :partner_email => '[email protected]')

# send email
Emailer.deliver_quotation_notification(q)

Then finally, I created a new model for QuotationEmail

class QuotationEmail

  def initialize(recipient_email, partner_name, partner_email)
    @recipient_email = recipient_email
    @partner_name = partner_name
    @partner_name = partner_email
  end

end

The error I get is ArgumentError (wrong number of arguments (1 for 3))

Eventually I'll be sending more parameters and hopefully attaching a pdf aswell but just trying to figure out why this isn't working first.

Upvotes: 0

Views: 100

Answers (1)

saihgala
saihgala

Reputation: 5774

You are getting this error because while initialising QuotationEmail object though you think you're passing 3 params you're essentially passing only one parameter which is a hash. And initialize is expecting 3. See example below

class A
  def initialize(a,b=1,c=2)
    puts a
    puts b
    puts c
  end
end

a = A.new(:recipient_email => '[email protected]', :partner_name => 'Martin Carlin', :partner_email => '[email protected]')

#=> {:recipient_email=>"[email protected]", :partner_name=>"Martin Carlin", :partner_email=>"[email protected]"}
#=> 1
#=> 2

If you're trying to use named parameters instead you'd need to redefine your initialize as

def initialize(recipient_email:a,partner_name:b,partner_email:c)

and invoke it as below -

a = A.new(recipient_email:'[email protected]', partner_name:'Martin Carlin', partner_email:'[email protected]')

Upvotes: 1

Related Questions