How to disable Turbolinks in a specific page other than using body tag?

Viewed 517
%div.col-md-2
  Last updated (h:m):
  %div
    %body{"data-turbolinks" => "false"}
      %strong= current_user.last_active_at ? local_time(current_user.last_active_at, '%d/%m/%Y %H:%M') : 'N/A'

Turbolinks is disabled when using "data-turbolinks" => "false" in the body tag and works fine but we cannot use body tag somewhere middle of the code, so trying it in any other tag like div does not work in disabling the turbolinks ,so is there any way of doing it without using body tag ?

1 Answers

You can do something like this:

In your app/helpers/application_helper.rb file, create a blacklist turbolinks array which will have all the pages on which turbolinks needs to be disabled in the format controller/action. Say you don't want turbolinks in welcome controller's index action, articles controller's show action:

# app/helpers/application_helper.rb

module ApplicationHelper

  def turbolinks_blacklist_array
    ["welcome/index", "articles/show"]
  end
end

Then in your app/views/layouts/application.html.erb:

  <body
    <% if turbolinks_blacklist_array.include?("#{params[:controller]}/#{params[:action]}") %>
      <%= 'data-turbolinks="false"'.html_safe %>
    <% end %>
  >
    <%= yield %>
  </body>

EDIT: Or use a onelines:

<body <%= turbolinks_blacklist_array.include?("#{params[:controller]}/#{params[:action]}") ? 'data-turbolinks="false"'.html_safe : '' %>>


</body>
Related