Answer the question
In order to leave comments, you need to log in
Caching by RoR book. Can you help me figure it out?
Please help me understand this code
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h1>Your Pragmatic Catalog</h1>
<% cache ['store', Product.latest] do %>
<% @products.each do |product| %>
<% cache ['entry', product] do %>
<div class="entry">
<%= image_tag(product.image_url) %>
<h3><%= product.title %></h3>
<%= sanitize(product.description) %>
<div class="price_line">
<span class="price"><%= number_to_currency(product.price) %></span>
<!-- START_HIGHLIGHT -->
<%= button_to 'Add to Cart', line_items_path(product_id: product) %>
<!-- END_HIGHLIGHT -->
</div>
</div>
<% end %>
<% end %>
<% end %>
def self.latest
Product.order(:updated_at).last
end
Answer the question
In order to leave comments, you need to log in
This caches snippets for each individual product and a snippet for the entire list of products.
def self.latest
Product.order(:updated_at).last
end
<% cache ['entry', product] do %>
...
<% end %>
<% cache ['store', Product.latest] do %>
...
<% end %>
There is no point in caching erb, see for yourself the rendering time of the view - it will not be faster for you to access the cache store. Cache the data in the controller.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question