Send an actionmailer email upon user registration

2019-06-10 20:03发布

问题:

I'm trying to send an email upon user registration in a rails 3 application that is using the devise gem. Every time I try to the send the email I get the following error:

NoMethodError in Devise::RegistrationsController#create undefined method `welcome_email' for UserMailer:Class

My app/model/user.rb has the following code...

after_create :send_welcome_email

  def send_welcome_email
    UserMailer.welcome_email(self).deliver
  end

My app/mailers/user_mailer.rb has the following code...

class UserMailer < ActionMailer::Base
  default from: "support@mysite.com"

  def welcome_email(user)
    @user = user
    @url  = "http://mysite.com/login"
    mail(:to => "#{user.email}", :subject => "Welcome to My Awesome Site")
  end
end

The method welcome_email exists so I'm not sure why I'm getting the error. Been trying to resolve this problem for the past couple of hours.

Thanks in advance for you help!! As always if you give me a good answer I will accept it as so. Alex

回答1:

Looks like the welcome_email is an instance method. And it looks like the error says it needs to be a class method. So try rewriting the method declaration as:

def self.welcome_email(user)

I guess that should solve it.