$(function() { // As documented on the reference below, turbolinks 5 does not treat a render // after a form submit by default, leaving the users to implement their own // solutions. // // https://github.com/turbolinks/turbolinks/issues/85#issuecomment-323446272 // // The code below imitates the behavior of turbolinks when treating regular GET // responses. Namely, it: // - Replaces only the body of the page // - It runs all script tags on the body of the new page // - It fires the turbolinks:load event // // This doesn't mean it does ALL what turbolinks does. For example, we don't // merge script tags from old and new page
elements. // This also doesn't change the browser history or does any change to the URL. // The reason we don't do such things is simply that this is a solution to // render errors in forms, and usually we render the same page/form rendered // before the submit. var handleResponse = function(response) { // parses response var newDom = new DOMParser().parseFromString(response.responseText, "text/html"); // Some browsers (PhantomJS and earlier versions of Firefox and IE) don't implement // parsing from string for "text/html" format. So we use an alternative method // described here: // https://developer.mozilla.org/en-US/Add-ons/Code_snippets/HTML_to_DOM#Parsing_Complete_HTML_to_DOM if (newDom == null) { newDom = document.implementation.createHTMLDocument("document"); newDom.documentElement.innerHTML = response.responseText; } if (newDom == null) { console.error("turbolinks-form was not able to parse response from server."); } // dispatches turbolinks event Turbolinks.dispatch('turbolinks:before-render', {data: {newBody: newDom.body}}); // console.log('before-render') // Removes/saves all script tags contents. // Most browsers don't run the new