Suppose that I have controller home_controller.rb
with action index
.
I want to cache index page so I\'m doing:
caches_page
The accepted answer is now out of date, as conditional caching is now available in Rails 4. A pull request was merged into 4.0.0rc1 that allows for :if
and :unless
parameters to be passed to cache
. This allows templates to be DRY, since there is no longer a need to duplicate the conditionally cached block.
Usage:
<% cache [ @user, 'form' ], :unless => @user.changed? do %>
<%= render partial: 'shared/error_messages', locals: {instance: @user} %>
<%= form_for @user do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.submit %>
<% end %>
<% end %>