是否有解决方法,用于使用caches_action和:layout =>忽略的content_for块?假? [英] Is there a workaround for ignored content_for blocks with caches_action and :layout => false?
问题描述
使用 caches_action:layout =>在Rails 3中返回false
,在缓存视图中填充并在布局中使用的所有content_for块最终都为空。
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!' %>
当然,如果使用:layout =>缓存操作, false
,这会导致页面标题空白,因为操作缓存会忽略所有content_for块。
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.
这是否没有解决方法?使用:layout =>进行操作缓存false
非常有用,但是这种故障使它很尴尬。
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
- 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
和轨道文档注意此行为:
警告:content_for在缓存中被忽略。因此,您不应将其用于将要片段缓存的元素。
"WARNING: content_for is ignored in caches. So you shouldn’t use it for elements that will be fragment cached."
推荐答案
我相信我解决这个问题的方法是
I believe the way I got around this was to create a fragment cache of the parts of the header that are dependent on the content_for being populated.
所以看起来像这样:
# application.html.erb
<head>
<% cache("#{request.env['PATH_INFO']}/header") do %>
<title><%= yield(:page_title) %></title>
<% end %>
因此应在操作被缓存的同时填充此缓存的片段。
so this cached fragment should be populated at the same time the action is cached.
这篇关于是否有解决方法,用于使用caches_action和:layout =>忽略的content_for块?假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!