📖 Scenario: You are building a blog page in a Rails app. The page shows a list of posts. To make the page faster, you want to cache each post's HTML fragment so Rails doesn't have to render it every time.
🎯 Goal: Build a Rails view that uses fragment caching to store each post's HTML snippet. This will speed up page loading by reusing cached HTML for posts that haven't changed.
📋 What You'll Learn
Create a list of posts as an array of hashes in the controller
Set a cache key prefix variable in the view
Use cache helper with the cache key prefix and post id in the view partial
Wrap the post content HTML inside the cache block in the partial
💡 Why This Matters
🌍 Real World
Fragment caching is used in Rails apps to speed up rendering of views by storing parts of HTML that don't change often. This reduces server load and improves user experience.
💼 Career
Understanding fragment caching is important for Rails developers to optimize app performance and scalability in real projects.
Progress0 / 4 steps
1
Set up posts data in the controller
In the controller, create a variable called @posts and assign it an array of hashes with these exact entries: { id: 1, title: 'Hello Rails' }, { id: 2, title: 'Fragment Caching' }, { id: 3, title: 'Speed Up Views' }.
Ruby on Rails
Hint
Use an instance variable @posts and assign it an array of hashes with the exact keys and values.
2
Add a cache key prefix variable in the view
In the view file, create a variable called cache_key_prefix and set it to the string 'posts_list'.
Ruby on Rails
Hint
Just assign the string 'posts_list' to a variable named cache_key_prefix.
3
Use cache helper with cache key prefix and post id
In the view, write a for loop iterating over @posts with variable post. Inside the loop, use the cache helper with the key [cache_key_prefix, post[:id]] to wrap the post's title inside an <h2> tag.
Ruby on Rails
Hint
Use a for loop with post over @posts. Use cache [cache_key_prefix, post[:id]] do to wrap the post title.
4
Complete the fragment caching with proper ERB tags
Ensure the view code uses proper ERB tags: open the Ruby code with <% %> and output the post title with <%= %>. The cache block should wrap the <h2> with the post title inside.
Ruby on Rails
Hint
Make sure to use ERB tags correctly: <% %> for Ruby code and <%= %> to output the post title.
Practice
(1/5)
1. What is the main purpose of fragment caching in Rails?
easy
A. To manage database connections efficiently
B. To store user login information securely
C. To save parts of a page so they load faster next time
D. To compress images for faster download
Solution
Step 1: Understand fragment caching concept
Fragment caching stores parts of a webpage to avoid regenerating them every time.
Step 2: Identify the main benefit
This speeds up page rendering by reusing cached parts instead of rebuilding them.
Final Answer:
To save parts of a page so they load faster next time -> Option C
Quick Check:
Fragment caching = save parts for speed [OK]
Hint: Fragment caching speeds up page parts loading [OK]
Common Mistakes:
Confusing caching with user authentication
Thinking it compresses images
Assuming it manages database connections
2. Which of the following is the correct syntax to use fragment caching in a Rails view?
easy
A. <% cache 'unique_key' do %> ... <% end %>
B. <% cache do 'unique_key' %> ... <% end %>
C. <% cache 'unique_key' %> ... <% end %>
D. <% cache 'unique_key' { ... } %>
Solution
Step 1: Recall correct fragment caching syntax
The correct syntax uses cache 'key' do ... end block in ERB.
Step 2: Check each option
<% cache 'unique_key' do %> ... <% end %> matches the correct block form. Others have wrong block usage or missing do.
Final Answer:
<% cache 'unique_key' do %> ... <% end %> -> Option A