When you use caches_action :layout => false
in Rails 3, any content_for blocks that are populated in the cached view and used in your layout wind up empty. Is there any workaround for this?
E.g. in my application I have the following rather typical setup.
A helper method called from my views which sets the page title:
# application_helper.rb
def page_title(title)
content_for(:page_title) { title }
end
A line in my layout file as follows
# application.html.erb
<head>
<title><%= yield(:page_title) %></title>
</head>
And in a view I might call
# index.html
<% page_title 'Hello!' %>
Of course, if you cache your action with :layout => false
, this results in having blank page titles, since action caching ignores all content_for blocks.
Is there no workaround for this? Action caching with :layout => false
is so close to being brilliantly useful, but this glitch renders it quite awkward.
Other folks asking or commenting about this same issue:
- http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/d8d72f050701d44b
- http://www.golygon.com/2011/04/tips-and-tricks-in-ruby-on-rails/
- https://rails.lighthouseapp.com/projects/8994/tickets/4140-action-caching-with-caches_action-and-layout-false
And the Rails documentation that notes this behavior:
"WARNING: content_for is ignored in caches. So you shouldn’t use it for elements that will be fragment cached."