Reputation: 1031
My app has a simple signup where the user types in his/her email address and POSTs the request. The request is then sent to my server using AJAX, an email is sent to the user's email using ActionMailer, and a thank you message is rendered using jQuery. With the code I have currently, the thank-you message is rendered only AFTER the email is sent, so it takes some time for the thank-you message to show. However, I'd like the thank-you message to be rendered first, and the email to be sent to the user in the background, so that the user can immediately know that his/her email was saved. Is there a way to process email in the background with Rails?
Below is my current code. In users_controller.rb
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'Thank you for signing up!' }
format.js
format.json { render json: @user, status: :created, location: @user }
Notifier.email_saved(@user).deliver
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
In mailers/notifier.rb
class Notifier < ActionMailer::Base
default from: "First Last <[email protected]>"
def email_saved(user)
@email = user.email
mail to: @email, subject: 'Auto-Response: Thank you for signing up'
end
end
In users/create.js.erb
$("<div class='alert alert-success'>Thank you for showing your interest! A confirmation email will be sent to you shortly.</div>").insertAfter("#notice");
Upvotes: 1
Views: 2280
Reputation: 56
If you want to send mail only, you should use better "Resque" or "Delayed Job" than "Ajax".
#271 Resque - RailsCasts http://railscasts.com/episodes/271-resque
Delayed Job (DJ) | Heroku Dev Center https://devcenter.heroku.com/articles/delayed-job
But if you want to send mail using Ajax, please use below snippets as a reference.
#app/controllers/users_controller.rb
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'Thank you for signing up!', sign_up_flag: 1 }
format.js
format.json { render json: @user, status: :created, location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
def send_mail(user_id)
begin
user = User.find(user_id)
Notifier.sign_up_mail(user.email).deliver
render :nothing => true, :status => 200
rescue
render :nothing => true, :status => 500
end
end
#app/mailers/notifier.rb
class Notifier < ActionMailer::Base
default from: "First Last <[email protected]>"
def sign_up_mail(email)
mail to: email, subject: 'Auto-Response: Thank you for signing up'
end
end
#app/views/???.html.erb
<% if @sign_up_flag == 1 %>
$(document).ready(function(){
$.ajax({
type: "POST",
url: "/sendmail",
data: "",
success : function(){},
error : function() {}
});
});
<% end %>
#config/routes.rb
post '/sendmail' => 'users#send_mail'
Thanks.
Upvotes: 2