gpt4 book ai didi

ruby-on-rails - rails : using "content_for" after the corresponding "yield" inside layout

转载 作者:行者123 更新时间:2023-12-04 01:45:50 27 4
gpt4 key购买 nike

我认为以前有人问过这个问题,但即使我搜索了谷歌,我也没有找到解决方案。

所以这就是我在 Rails 2.3.5 中尝试做的事情:

layouts/application.html.erb:
<html>
<head>
... some other stuff
<%= yield :head %>
</head>
<body>
<% content_for :head, "something that belongs in the head" %>
</body>
</html>

注意 content_for 之前的产量。

我知道 Rails - 默认情况下 - 不允许在使用 yield 后定义 :head 的内容 - 是有道理的。

我什至尝试加入模板渲染过程,但到目前为止没有成功。

所以我的目标是能够在部分/模板中定义 content_for 并在响应发送到浏览器之前以某种方式延迟和执行“yield”。

有人提出解决方案吗?

问候和感谢,
坦率

更新
我会按照 weppos 的想法去尝试机架中间件。谢谢

最佳答案

渲染过程首先加载并执行 Action 模板,然后用选定的布局装饰模板。
布局从上到下呈现,因此您无法向 :head 添加更多内容在 :head 之后已经渲染了。

你需要改变你的策略。要么将片段放在部分中并将其附加到您的操作 View ,要么使用后处理策略(例如 Rack 模块/after_filter)直接更改 html 代码。

我可能会尝试根据我的实际需要找到更好的解决方案。如果您遇到此问题,则错误可能在其他地方,可能在应用程序架构中。

关于ruby-on-rails - rails : using "content_for" after the corresponding "yield" inside layout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2026740/

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