相关疑难解决方法(0)

是否有使用caches_action和:layout => false忽略content_for块的解决方法?

caches_action :layout => false在Rails 3中使用时,在缓存视图中填充并在布局中使用的任何content_for块都将为空.这有什么解决方法吗?

例如,在我的应用程序中,我有以下相当典型的设置.

从我的视图调用的辅助方法设置页面标题:

# application_helper.rb
def page_title(title)
    content_for(:page_title) { title }
end
Run Code Online (Sandbox Code Playgroud)

我的布局文件中的一行如下

# application.html.erb
<head>
<title><%= yield(:page_title) %></title>
</head>
Run Code Online (Sandbox Code Playgroud)

在一个视图中我可以打电话

# index.html
<% page_title 'Hello!' %>
Run Code Online (Sandbox Code Playgroud)

当然,如果您使用缓存操作:layout => false,则会导致显示空白页面标题,因为操作缓存会忽略所有content_for块.

对此没有解决方法吗?动作缓存:layout => false非常接近于非常有用,但是这个故障使得它非常笨拙.

其他人询问或评论同一问题:

  1. http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/d8d72f050701d44b
  2. http://www.golygon.com/2011/04/tips-and-tricks-in-ruby-on-rails/
  3. https://rails.lighthouseapp.com/projects/8994/tickets/4140-action-caching-with-caches_action-and-layout-false

以及记录此行为的Rails文档:

"警告:在缓存中忽略了content_for.所以你不应该将它用于将被片段缓存的元素."

caching ruby-on-rails ruby-on-rails-3

22
推荐指数
1
解决办法
2352
查看次数

标签 统计

caching ×1

ruby-on-rails ×1

ruby-on-rails-3 ×1