How to disable Turbolinks in a specific page?

前端 未结 6 1649
醉酒成梦
醉酒成梦 2021-02-12 13:54

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

相关标签:
6条回答
  • 2021-02-12 14:25

    this is going sound really simple

    <%- if params[:controller] == 'controller_name' and params[:action] == 'index' %>
      <body data-no-turbolink="true">
    <%- else %>
      <body data-no-turbolink="false">
    <%- end %>
    

    I hope this help.

    0 讨论(0)
  • 2021-02-12 14:32

    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.

    <meta name="turbolinks-visit-control" content="reload">
    

    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.

    0 讨论(0)
  • 2021-02-12 14:32

    For the last Turbolinks, I've to add both add:

    <meta name="turbolinks-visit-control" content="reload">
    

    in the <head> and:

     <body data-turbolinks="false">
    

    in place of the <body> tag to fully disable Turbolinks for a specific page.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-12 14:36

    For turbolinks 5 if you use this method mentioned above:

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

    You'll need to change it to:

        <%= link_to "Policy", policy_path, :"data-turbolinks" => false %>
    
    0 讨论(0)
  • 2021-02-12 14:38

    To turn off turbolink for a specific link

    <a href="/" data-turbolinks="false"></a>
    
    0 讨论(0)
提交回复
热议问题