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: "[email protected]"
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 => '[email protected]', :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?
Confirmation emails should be sent from the controller. It's simple to override the Devise::RegistrationsController
defaults.
Create the file app/controllers/my_registrations_controller.rb
(name this whatever you want)
class MyRegistrationsController < Devise::RegistrationsController
def create
super
if @user.persisted?
UserMailer.new_registration(@user).deliver
end
end
end
Then in your routes:
devise_for :users, :controllers => { :registrations => "my_registrations" }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With