I’m building a prelaunch signup site with this tutorial. http://railsapps.github.com/tutorial-rails-prelaunch-signup.html. After a user enters their email to request an invite, it uses ajax to render a _thankyou.html partial, which is rendered from the overridden Devise registrations controller.
However, the site also uses an after_create :send_welcome_email callback to send a welcome ‘thanks for requesting the invite’ email. This slows down the performance of the ajax significantly. Without the after_create call back, the ajax (at least on local host) works very fast, the form disappears quickly and the thank you partial is rendered almost instantly. However, if I include the after_create callback, the form takes a long time to disappear (which will cause users to click the submit button multiple times, creating more problems) and the thankyou partial also takes a long time…
Is there a way to arrange things so that the email gets triggered after the thank you partial is rendered?
The new devise create method
# ovverride #create to respond to AJAX with a partial
def create
build_resource
if resource.save
if resource.active_for_authentication?
sign_in(resource_name, resource)
(render(:partial => 'thankyou', :layout => false) && return) if request.xhr?
respond_with resource, :location => after_sign_up_path_for(resource)
else
expire_session_data_after_sign_in!
(render(:partial => 'thankyou', :layout => false) && return) if request.xhr?
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
render :action => :new, :layout => !request.xhr?
end
end
The ajax triggered on the request invite submit.
// use AJAX to submit the "request invitation" form
$('#invitation_button').live('click', function() {
var email = $('form #user_email').val();
var password = $('form #user_password').val();
var dataString = 'user[email]='+ email + '&user[password]=' + password;
$.ajax({
type: "POST",
url: "/users",
data: dataString,
success: function(data) {
$('#request-invite').html(data);
}
});
return false;
});
my guess is sending the email is what takes the most time, try using delayed_job or some other background processing to do the actual sending of the email, so the after_create would just queue the email to be sent, but it would actually be sent by a background process
https://github.com/collectiveidea/delayed_job