I have developed a small Demo App to testing some interfaces API with the social media like Facebook and Twitter.
I have a "problem" when in the view I do an Ajax-json call with the turbolinks
gem actived. Sometimes (yes, sometimes!) it render the JSON response in the view (like a .json) instead the right page (html). If I disable the turbolinks
gem the problem doesn't appears anymore.
Anyone has an idea why this happens? And what can I do to avoid this problem even with the turbolinks
gem?
If you need some code to better understand it, you can find the call that does (sometimes) the error here (check the Ajax call): Link
The problem will be that Turbolinks will reload the page via Ajax most of the time, meaning the typical javascript DOM loading process does not occur
Delegation
It sounds to me like you've bound your ajax process to a particular element, like this:
$("element").on("click", function() {
$.ajax({....
...});
});
The way around this is to use javascript delegation to bind elements to the document, and delegate to the elements you need
Looking at your code, I'd do this:
$(document).on("submit", "form", function(){
// .... your ajax code
});
I would also ensure you use unobtrusive javascript (put it in it's own JS file, rather than your view). If you gave us more context, I could create a more tailored solution for you