Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to cache a post partial using Russian doll caching.
Ruby on Rails
<%= cache(@post) do %> <%= [1] 'post' %> <% end %>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
link_to instead of render inside the cache block.Forgetting to render the partial inside the cache block.
✗ Incorrect
The
render method is used inside the cache block to render the post partial, enabling Russian doll caching.2fill in blank
mediumComplete the code to cache a collection of comments with Russian doll caching.
Ruby on Rails
<%= cache(@comments) do %>
<%= [1] @comments %>
<% end %> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
link_to or other helpers instead of render.Not rendering the collection inside the cache block.
✗ Incorrect
Rendering the collection inside the cache block caches each comment efficiently using Russian doll caching.
3fill in blank
hardFix the error in the code to properly use Russian doll caching for a post and its comments.
Ruby on Rails
<%= cache(@post) do %>
<h2><%= [1] %></h2>
<%= render @post.comments %>
<% end %> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variable
post instead of @post.Using incorrect syntax like
post[:title].✗ Incorrect
Using
@post.title correctly accesses the post's title inside the cache block.4fill in blank
hardFill both blanks to use a cache key with a timestamp for Russian doll caching.
Ruby on Rails
<%= cache([@post, [1]]) do %> <%= render 'post', post: @post %> <% end %>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
@post.created_at which doesn't change after creation.Using
Time.now which changes every time and breaks caching.✗ Incorrect
Using
@post.updated_at as part of the cache key ensures the cache updates when the post changes.5fill in blank
hardFill all three blanks to implement nested Russian doll caching for a post and its comments.
Ruby on Rails
<%= cache(@post) do %> <h1><%= [1] %></h1> <ul> <% @post.comments.each do |comment| %> <li><%= [2](comment) do %> <p><%= [3] %></p> <% end %></li> <% end %> </ul> <% end %>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not caching each comment individually.
Using
render instead of cache for comments.Not displaying the comment body correctly.
✗ Incorrect
The post title is shown, each comment is cached with
cache(comment), and the comment body is displayed inside.