Rails - Devise send user email after sign_up/create

ruby-on-rails, ruby-on-rails-4

Solution

When users sign up with Devise, they don't go through the `UsersController`.

You might want to add the mail sending code in the `User` model.

For example, in `app/models/user.rb`:

class User < ActiveRecord::Base
  # ...

  after_create :send_admin_mail
  def send_admin_mail
    UserMailer.send_new_user_message(self).deliver
  end

  # ...
end

This is done by utilizing the Active Record `after_create` callback.

Problem

I'm fairly new to rails and trying to figure things out. I recently got a mailer all setup and it was working fine. But I am trying to add a second mailer for user actions and it doesnt seem to work. What I am trying to achieve is that a user signs up, it sends an email to the administrator, then the admin must enable the user. Currently when the admin enables the user it will send them an email, but for some reason my newly created user email does not fire. I think this is because my create method does not fire, where should I put it? Do I need to overwrite a user method? My UserMailer controller: ``` class UserMailer < ActionMailer::Base default from: "website@rnd.designcom.com.au" def send_enabled_message(user) @user = user mail(:to => user.email, :subject => "Welcome to Pixel Technologies!!!") end def send_new_user_message(user) @user = user mail(:to => 'ben.suryn@rnd.designcom.com.au', :subject => "New User created please review and enable.") end end ``` My users_controller: ``` class UsersController < ApplicationController before_filter :authenticate_user! load_and_authorize_resource # POST /users def create @user = User.new(user_params) puts "******************** NEW USER ****************************" puts user_params if @user.save puts 'Sending email for create user' UserMailer.send_new_user_message(@user).deliver redirect_to @user, notice: 'User was successfully created.' else render action: 'new' end end ``` But this create method never gets fired. What am I donig wrong. Is there another method I need to put UserMailer.send_new_user_message(@user).deliver?

Original source