Rails 4: Disable Turbolinks in a Specific Page

Rails 4: disable Turbolinks in a specific page

Add “data-no-turbolink” to the <body> tag of the the page you want it disabled on

If you have a shared layout file which i am assuming you do, you can do an if statement and check the params[:controller] and params[:action] and just add it to the one area

Edited Jan 2021: As pointed out by @weltschmerz under rails 5 data-turbolinks="false" is the preference.

How to disable Turbolinks in a specific page?

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.

How to disable TurboLinks in Rails 6?

  1. Remove gem 'turbolinks', '~> 5' from Gemfile
  2. Remove //= require turbolinks from app/assets/javascript/application.js
  3. Remove , 'data-turbolinks-track': 'reload' (x2) from app/views/layouts/application.html.erb
  4. Run yarn remove turbolinks
  5. Run rails tmp:cache:clear

Step 4 is the main difference with Rails 5.

How to disable turbolinks for pagination links created by Kaminari gem

I think you should create custom views (bootstrap-4 is only example, you can choose something different):

rails g kaminari:views bootstrap4

This generates folder kaminari and all required views. Then you can customise links, in your case by adding additional attribute,

<li class="page-item">
<%= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, rel: 'next', remote: remote, class: 'page-link', data: { turbolinks: false } %>
</li>

How to disable Turbolinks for single page in a shared view?

current_page?(view_path) will check your if statement(not enought rep for comment this)

Rails: How to disable turbolinks in Rails 5?

Basically straight from here. It's for Rails 4, but I believe the steps are the same.

1) Remove the gem 'turbolinks' line from your Gemfile.

2) Remove the //= require turbolinks from your app/assets/javascripts/application.js .

3) Remove the two "data-turbolinks-track" => true hash key/value pairs from your app/views/layouts/application.html.erb .

Edit: As of at least Rails 5.0.0 the last step should refer to "data-turbolinks-track" => "reload" as opposed to "data-turbolinks-track" => true. Thanks to @boddhisattva

Edit: As of at least Rails 4.2 you can generate a project without turbolinks to begin with. Just use something like this:

rails new my_app --skip-turbolinks



Related Topics



Leave a reply



Submit