Send an actionmailer email upon user registration in devise rails 3 application

China☆狼群 提交于 2019-12-08 12:30:23

问题


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.



来源:https://stackoverflow.com/questions/14668920/send-an-actionmailer-email-upon-user-registration-in-devise-rails-3-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!