【问题标题】:content_for works in development but not production?content_for 在开发中工作但不是在生产中工作?
【发布时间】:2012-06-25 03:59:45
【问题描述】:

我有一个使用 caches_action 的控制器

controllers/bar_controller.rb:

caches_action :bar, :layout => false

鉴于这个动作,我会在布局中设置html标题。

views/foo/bar.html.erb:

<%= content_for :mytitle do "testing" end %>

这是我的布局文件:

views/layouts/application.html.erb:

<title><%= yield :mytitle %></title>

但是,这仅在开发中有效。在生产中,它不起作用。任何想法都值得赞赏。谢谢。

类似问题:Is there a workaround for ignored content_for blocks with caches_action and :layout => false?

【问题讨论】:

  • 缓存可能正在开发中。当它确实在生产环境中缓存时,您是在告诉它忽略布局,并使用它从content_for 呈现什么。
  • 缓存中是否忽略 content_for?
  • 如果你在布局中产生 content_for 并传递:layout =&gt; false,看起来是这样。来自the docs:“如果你传递:layout => false,它只会缓存你的动作内容。当你的布局有动态信息时这很有用。”

标签: ruby-on-rails caching content-for


【解决方案1】:

Rails 在启用缓存的环境中将简单地忽略 content_for 块。使用ActionController::Filters 计算值并将这些值存储在实例变量中。在布局中引用实例变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 2015-02-23
    • 2013-06-20
    • 1970-01-01
    • 2015-08-05
    相关资源
    最近更新 更多