gpt4 book ai didi

ruby-on-rails - content_for 与片段缓存结合使用的解决方法

转载 作者:行者123 更新时间:2023-12-03 19:47:00 27 4
gpt4 key购买 nike

我试图在每个元素的基础上将内容插入到我的页面的头部,所以我希望能够在部分指定这样的内容:

# _partial.html.erb
<%= content_for :style %>
.element {
background-color: red;
}
<% end %>

并将其放在我的页面的头部:
# application.html.erb
<head>
<style>
<%= content_for(:style) %>
</style>
</head>

但是元素部分是片段缓存的, content_for在缓存中被忽略。

Rails 文档中指出 content_for不会用于片段缓存的工作元素:
WARNING: content_for is ignored in caches. So you shouldn't use it for elements that will be fragment cached. http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for

目前有没有办法获得 content_for在Rails 5.1 上使用片段缓存?似乎已经有一段时间没有人真正触及这个问题了。有谁知道是否有原因?

这里有一些较旧的提及:

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

content_for works in development but not production?

https://gist.github.com/stackng/891895

https://rails.lighthouseapp.com/projects/8994/tickets/3409-content_for-and-fragment-caching

最佳答案

现在有这个开放的 Rails 拉取请求:https://github.com/rails/rails/pull/39600

希望这将很快在 Rails 中“正常工作”...... 🤞

免责声明:我提交了这个拉取请求

关于ruby-on-rails - content_for 与片段缓存结合使用的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51433557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com