Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to cache a fragment named 'menu' in a Rails view.
Ruby on Rails
<% cache [1] do %>
<nav>Menu content here</nav>
<% end %> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol instead of a string for the cache key.
Omitting quotes around the cache key.
✗ Incorrect
In Rails views, fragment caching uses a string key inside the cache helper, so 'menu' as a string is correct.
2fill in blank
mediumComplete the code to cache a fragment with a dynamic key using a variable 'product'.
Ruby on Rails
<% cache [1] do %>
<div><%= product.name %></div>
<% end %> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'product' which caches the whole object incorrectly.
Using product.id alone without context.
Using a plain string 'product' which is static.
✗ Incorrect
Using an array like [product, 'details'] creates a cache key that changes when the product changes, which is best practice.
3fill in blank
hardFix the error in the fragment caching code by completing the blank.
Ruby on Rails
<% cache [1] do %>
<p>Cached content here</p>
<% end %> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using product.id which does not change when product updates.
Using product.to_s which is not a cache key.
Using cache_key without version may cause stale cache.
✗ Incorrect
Using product.cache_key_with_version ensures the cache key changes when the product or its version changes, avoiding stale cache.
4fill in blank
hardFill both blanks to cache a fragment with a custom expiration time of 10 minutes.
Ruby on Rails
<% cache({ key: [1], expires_in: [2] }) do %>
<section>Expiring cache content</section>
<% end %> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number without .minutes for expiration.
Using a symbol instead of a string for the key.
Setting expiration time too short or too long.
✗ Incorrect
The key 'sidebar' identifies the fragment, and expires_in: 10.minutes sets the cache to expire after 10 minutes.
5fill in blank
hardFill all three blanks to cache a fragment with a key based on 'user', include a version, and set expiration to 30 minutes.
Ruby on Rails
<% cache(key: [1], version: [2], expires_in: [3]) do %> <article>User profile content</article> <% end %>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using user.id alone without versioning.
Using a string instead of an array for the key.
Omitting expiration or version leading to stale cache.
✗ Incorrect
The key is an array with user to generate a unique cache key, version uses user.cache_version for freshness, and expires_in sets 30 minutes expiration.