Tuesday, May 21, 2024
14
rated 0 times [  18] [ 4]  / answers: 1 / hits: 21326  / 10 Years ago, fri, november 28, 2014, 12:00:00

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>

More From » ruby-on-rails

 Answers
22

You can use this oneliner in your layout:



<body <%= data-no-turbolinks='true'.html_safe if controller_name==pages && action_name==policy %>>


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:



<%= link_to Policy, policy_path, :data-no-turbolink => true %>


Long time has gone, here is an update



Recently, I have started using turbolinks 5.0 beta, by:



gem 'turbolinks', '~> 5.0.0.beta'


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.



$(document).on('turbolinks:load', named_function );
var named_function = function() {
// thinks to do on document load
}


You don't have to also add



$(document).ready(function (){
// stuff
});


or



$(document).ready(named_function);


Because Turbolinks will gracefully fall back to document load if the page is hard loaded.


[#68665] Wednesday, November 26, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myrap

Total Points: 407
Total Questions: 105
Total Answers: 109

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
myrap questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Wed, Jan 15, 20, 00:00, 4 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
Thu, Oct 3, 19, 00:00, 5 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;