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 <my@email.com>"
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");
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.
Thanks.