Skip to content

Instantly share code, notes, and snippets.

@jheg
Created July 1, 2015 21:26
Show Gist options
  • Save jheg/df2e9ed6afb51c91af9f to your computer and use it in GitHub Desktop.
Save jheg/df2e9ed6afb51c91af9f to your computer and use it in GitHub Desktop.
caching in Rails 4 book Agile Web Development with Rails 4
# in development.rb
config.action_controller.perform_caching = true
# in app/models/product.rb
def self.latest
Product.order(:updated_at).last
end
# in app/views/store/index.html.erb
<% 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>
</div>
</div>
<% end %>
<% end %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment