Rails - 设计在注册/创建后发送用户电子邮件 [英] Rails - Devise send user email after sign_up/create
问题描述
我对 Rails 还很陌生,正在努力解决问题.我最近安装了一个邮件程序,它运行良好.但我正在尝试为用户操作添加第二个邮件程序,但它似乎不起作用.
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.
我想要实现的是,用户注册后,它会向管理员发送一封电子邮件,然后管理员必须启用该用户.目前,当管理员启用用户时,它会向他们发送一封电子邮件,但由于某种原因,我新创建的用户电子邮件不会触发.我想这是因为我的 create 方法没有触发,我应该把它放在哪里?我需要覆盖用户方法吗?
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?
我的 UserMailer 控制器:
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
我的 users_controller:
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
但是这个 create 方法永远不会被触发.我做错了什么.是否有另一种方法需要放置 UserMailer.send_new_user_message(@user).deliver?
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?
推荐答案
当用户注册 Devise 时,他们不会通过 UsersController
.
When users sign up with Devise, they don't go through the UsersController
.
您可能希望在 User
模型中添加邮件发送代码.
You might want to add the mail sending code in the User
model.
例如,在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
这是通过使用 Active Record after_create
回调来完成的.
This is done by utilizing the Active Record after_create
callback.
这篇关于Rails - 设计在注册/创建后发送用户电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!