0
0
Ruby on Railsframework~10 mins

Fragment caching in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A'menu'
Bmenu
C:menu
D"menu"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a symbol instead of a string for the cache key.
Omitting quotes around the cache key.
2fill in blank
medium

Complete 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'
Aproduct.id
B'product'
Cproduct
D[product, 'details']
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.
3fill in blank
hard

Fix 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'
Aproduct.cache_key_with_version
Bproduct.cache_key
Cproduct.id
Dproduct.to_s
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.
4fill in blank
hard

Fill 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'
A'sidebar'
B10.minutes
C5.minutes
D'header'
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.
5fill in blank
hard

Fill 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'
A[user]
Buser.cache_version
C30.minutes
Duser.id
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.