Dinesh Saini
Dinesh Saini

Reputation: 2916

Ruby on Rails 5 - Turbolink 3 did not work correctly

I am using the Ruby on Rails 5 and use turbo-link as shown below:

<%= javascript_include_tag 'custom-plugins', 'data-turbolinks-track' => true %>

It is working fine with Ruby On Rails 4 but have issue with Ruby on Rails version 5. Once I click on back the js/css do not loaded correctly.

Any help will be appreciated.

Upvotes: 6

Views: 866

Answers (2)

Sumit Munot
Sumit Munot

Reputation: 3868

As Refereed to Rails 5 Awesome features

Turbolinks has been part of Rails since version 4, probably one of the features that people hate it or love; there is no middle ground here.

With Rails 5 we will be receiving a new version that, with the help of HTML5 custom data attributes, we will expect better speed and rendering in our Rails applications.

The most significance change in this new version is the Partial Replacement feature. From the client side, we will be able to tell Turbolinks what content do we need to change/replace and what we don’t.

Turbolinks will look for HTML5 custom attributes and to decide the replacement strategy in our .

To trigger a replacement in the client side we could use or to update our . The difference between and is that the first one will issue a to the server to obtain the HTML that must be used to replace our while expects from us the HTML that should be used for its operation.

With both functions, we can pass a hash with an or an array of of HTML elements to or.

Action  Result
Turbolinks.visit(url, { change: ['entries'] })  Will replace any element with custom attribute and any element with its id listed in change.
Turbolinks.visit(url)   Will keep only elements with custom attribute and replace everything.
Turbolinks.visit(url, { keep: ['flash'] })  Will keep only elements with custom attribute and any element with its id listed in keep, everything else will be replaced.
Turbolinks.visit(url, { flush: true })  Will replace everything

We can trigger the same functionality from the server-side with and , both can receive , and as options but can also receive with or to force a redirect with or without Turbolinks.

Whether you like Turbolinks or not, this might be a good time to try out and find out if it could be a good fit somewhere in your application.

Upvotes: 2

Pinasis
Pinasis

Reputation: 33

It is a common occurrence in for turbolinks with js. Turbolinks helps load a particular page a lot faster. But what it also does is stops the js from functioning sometimes. So while loading this particular page use this line

<%= link_to "example_page", example_page_path, :"data-no-turbolink" => true %> 

or write this in your layout

<body <%= "data-no-turbolinks='true'".html_safe if controller_name=="example_controller" && action_name=="example_page" %>>

to stop turbolink from working in this particular page.

Upvotes: 1

Related Questions