May 2025
Intermediate to advanced
270 pages
6h 55m
English
Historically, Rails has supported different ways of caching. Nowadays, the most popular way of caching that’s currently supported by vanilla Rails is fragment caching. Let’s use it to make our application a bit better!
Let’s go back to our view in app/views/films/index.html.erb. Using fragment caching is as simple as adding a call to the cache[23] method in one line of our file:
| | # app/views/films/index.html.erb |
| | |
| | <% cache @films do %> |
| | <% @films.each do |film| %> |
| | <div class="film"> |
| | <h2><%= film.title %></h2> |
| | <p> |
| | <%= "Language: #{film.language.name}" %> |
| | </p> |
| | <p> |
| | <%= "Available at #{film.stores.pluck(:name).join(', ')}" %> |
| | </p> |
| | </div> |
| | <% ... |
Read now
Unlock full access