I have been experiencing an intermittent issue in my Rails app, and I'm having trouble figuring out what is going on. When the user logs in, they see a dashboard that contains some JavaScript code which performs an AJAX call to an action. Occasionally, instead of seeing the dashboard, when the user logs in, they see the JSON response text from the action instead of the dashboard (in Chrome) or they download a .json file (Firefox). It's intermittent and doesn't usually happen, but it's really annoying when it does occur.
Here's a dumbed down version of some of the code:
JS running in the template head:
$(function () {
var remoteLink = $('#remoteLink');
remoteLink.live("ajax:complete", function () {
setTimeout(function () {
loadCount();
}, 30000);
});
loadCount();
function loadCount() {
remoteLink.click();
}
});
And the link in the template:
<%= link_to 'get count (hidden)', {:controller => 'something', :action => 'count'},
:id => 'remoteLink', :class => 'hidden', :remote => true, 'data-type' => 'json' %>
And the controller action:
def count
render :json => get_counts_function_returning_a_hash
end
My hunch is that it's a race condition -- perhaps related to the use of setTimeout? -- but I haven't been able to verify that hunch. Can anyone tell me what might be going on here? I've seen this in the wild on other sites, too -- also intermittent, not generally occurring but annoying when it happens.
Do you have any authentication code that uses store_location or similar functionality to redirect the user to a specific page after they have logged in?
I once had a similar problem where the AJAX callback was causing store_location to store the JSON URL and the user was redirected to the JSON response rather than their dashboard. This was hard to track down because the user had to sit idle at the page for some time before the bug was manifest.
In any case, I suspect it is something like this rather than a problem with Rails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With