Caution: Ajax and Redirect Back

Caution: Be careful with AJAX calls and redirecting! It took me a
while to figure out why some customers were being emailed multiple times
until I finally figured out the steps to reproduce the problem.

For example - On a detail (show) form, I would click a button to email
them a document. This would invoke an ajax remote call and update the
UI with the status. Then, I would go and create an activity (from the
show form). In the normal course of events, after an activity is
created, I want to redirect back to the show form. In this case, since
the email was just sent, that was the last action before the activity
creation. Thus, the redirect back launched the ajax call again instead
of doing what I wanted - which is showing the form.

This was somewhat transparent in that the show form still came up! Just
an FYI for anyone who may be experiencing anything like it.



:slight_smile: Sorry! No good to post a problem w/o a solution!

If you use the session data or custom redirect logic (not necessarily
assuming back button) (i.e. an application helper) then the other
solution is to correct the entries, session or otherwise, in the action
being called by the remote call. The point of my warning is that this
is easy to forget when making an ajax call because the UI isn’t
navigating away from where it was!



And the solution would be to set history.previous manually after
your AJAX call (for those wondering).