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":

<div id="friend-count" data-turbolinks-permanent>3 friends</div>
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Adding the data-turbolinks-permanent attribute, we're telling turbolinks to persist certain elements across page loads

  • The official docs say:
Designate permanent elements by giving them an HTML id and annotating them with data-turbolinks-permanent. Before each render, Turbolinks matches all permanent elements by id and transfers them from the original page to the new page, preserving their data and event listeners.

This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to Persisting Elements Across Page Loads in Ruby on Rails