Ruby on Rails - persisting elements across page loads in ruby on rails- ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Consider the following situation:
Imagine that we are the developer of a social media website that allows users to be friends with other users and that employs turbolinks to make page loading faster. In the top right of every page on the site, there is a number indicating the total number of friends that a user currently has. Imagine we are using our site and that we have 3 friends. Whenever a new friend is added, we have some javascript that runs which updates the friend counter. Imagine that we just added a new friend and that our javascript ran properly and updated the friend count in the top right of the page to now render 4. Now, imagine that we click the browser's back button. When the page loads, we notice that the friend counter says 3 even though we have four friends.
- This is a relatively common problem and one that turbolinks has provided a solution for.
- The reason the problem occurs is because turbolinks automatically loads pages from the cache when a user clicks the back button. The cached page won't always be updated with the database.
Imagine that we render the friend count inside a <div> tag with an id of "friend-count":
Adding the data-turbolinks-permanent attribute, we're telling turbolinks to persist certain elements across page loads
- The official docs say: