Created
July 1, 2015 21:26
-
-
Save jheg/df2e9ed6afb51c91af9f to your computer and use it in GitHub Desktop.
caching in Rails 4 book Agile Web Development with Rails 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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