Search code examples
ruby-on-railsruby-on-rails-4turbolinks

Never use Turbolinks to load a specific route


I know that I can use the data-no-turbolink attribute to ensure that a link (or many links) don't use turbolinks, but is there any way to ensure that a specific page will never be loaded via turbolinks?

I have a particular page that is broken when loaded via turbolinks, and instead of employing some hacks to fix this, I'd rather that this page just always load in the usual fashion.

I know I can add data-no-turbolink to all links going to this page, but then I'd have to remember to do this to every subsequent link to this page, which is pretty hacky.

Is there a better way?


Solution

  • To modify all new and existing URLs to remove turbolinks to a specific URL, read below:

    Since you're wanting to always use "data-no-turbolinks" when linking to a specific page via link_to, you can modify the method to check the URL of its destination and set the data-no-turbolinks attribute.

    Modify the link_to helper (be sure to replace ROUTE_url and ROUTE_path with the correct variables from your routes:

    # application_helper.rb
    def link_to(name, path, options = {})
      no_turbolink_routes = [ROUTE_url, ROUTE_path]  # Add URLs/PATHs here
    
      if no_turbolink_routes.include? path
        options['data-no-turbolinks'] = 'true'
      end
    
      super(name, path, options)
    end