i was wondering how/where to write a callback function after an ajax request is complete?
im building a twitter app and im trying to send an email notification after a user clicks ‘follow’. the following/unfollowing is done using ajax. when a user clicks follow, i have…
def follow!(other_user)
relationships.create!(followed_id: other_user.id)
end
which in turns calls
def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
respond_to do |format|
format.html {redirect_to @user}
format.js
end
UserMailer.is_now_following(@user, current_user).deliver
end
however, by adding that UserMailer line, it lags my ajax a lot. where/how should i put the email request instead? i was thinking of a callback function after the ajax is finished, but im not sure where to add it or how.
sorry i still quite new to this. thanks a lot!
hmmm, i found out that maybe a good way to do this is to use jquery. i decided to give the follow button
<%= f.submit "Follow", :class => "btn btn-large btn-primary",
:id => "follow_button"%>
an id and used jquery
$("#follow_button").bind('ajax:success', function() {
});
to send the email afterwards. however, im really sure how i could pass in variables. the ultimate line i want to achieve would be
UserMailer.is_now_following(@user, current_user).deliver
current_user is a function btw which assigns/returns the current_user
how would i do this? thanks!
These railscasts series are pretty old, but may be helpful
http://railscasts.com/episodes/127-rake-in-background
http://railscasts.com/episodes/128-starling-and-workling
http://railscasts.com/episodes/129-custom-daemon