I have an issue with a script only working when refreshing the page and so I'm trying to disable Turbolinks for only that page. The code below doesn't work. However, if I add the "data-no-turbolink" attribute directly to the body tag in application.html.erb it works. How do I disable Turbolinks in my view? I have followed the solution posted here, Rails 4: disable Turbolinks in a specific page but I can't get it to work.
I have the gem 'jquery-turbolinks' installed.
policy.html.erb
<% content_for :body do %>
<% if controller.controller_name == 'pages' && controller.action_name == 'policy' %>
<body data-no-turbolink="true">
<% end %>
<% end %>
<div class="row">
<div class="small-12 medium-8 large-7 columns end">
<a href="//www.com/" class="nostyle" title="Policy">Policy</a>
<script>(function (w,d) {var loader = function () {var s = d.createElement("script"), tag = d.getElementsByTagName("script")[0]; s.src = "//cdn.java.com/java.js"; tag.parentNode.insertBefore(s,tag);}; if(w.addEventListener){w.addEventListener("load", loader, false);}else if(w.attachEvent){w.attachEvent("onload", loader);}else{w.onload = loader;}})(window, document);
</script>
</div>
</div>
For turbolinks 5 if you use this method mentioned above:
You'll need to change it to:
I tried the other methods mentioned here to no avail, but from the Turbolinks 5 docs, you can insert this meta tag on the page that you want to always load without Turbolinks.
Adding a data attribute to all links means that you have to know where all those links are, which is brittle if you have a situation where there might be inline links in user-created content.
The body tag data attribute method didn't work for me as I'd expect it to - I'd have assumed that loading a turbolinks-supported page and then clicking a link to go to a page where you want to disable turbolinks would result in a full, standard page load. That didn't seem to be the case, especially with back button or history navigation.
Adding this meta tag forces Turbolinks 5 to do a full page reload before it attempts to insert any content into the dom. You get that classic flash of a blank page, but if like me, you're using external Javascript that needs to fire without interference from Turbolinks, that's the desired behaviour.
You can use this oneliner in your layout:
But the other way to do it, which I use mostly, would be to put this chunk of code to the links leading to this page...
So, let's suppose your route is named
:policy
, you should do this:Long time has gone, here is an update
Recently, I have started using
turbolinks 5.0 beta
, by:It gets far easier... All
document ready
javascript gets loaded, no problem... All you have to do is add a listener to the load event.You don't have to also add
or
Because
Turbolinks
will gracefully fall back todocument load
if the page is hard loaded.this is going sound really simple
I hope this help.