2

我有一个使用布局的 Rails 应用程序

简化版如下所示:

<html>
  <head>
    <%= render 'layouts/head' # renders the "layouts/_head.html.erb" partial correctly  
                              # the most head (css/js) content gets implemented here %>
  </head>
  <body>

    <%= yield # renders the current action %>

    <!-- implement alls scripts which should be exectued on the bottom of the page -->
    <%= content_for :scripts_bottom %>
  </body>
</html>

在我的“layouts/_head.html.erb”中,我使用

 <%= content_for :scripts_head %>
 <!-- it seems `content_for` instead of `yield` appends content -->

在我partials的地方,我放置了以下片段来附加它们:scripts_head。(我的一些部分应该放 javaScripts

<% content_for :scripts_head do %>
    <%= javascript_include_tag 'some_script' %>
<% end %>

`layouts/head' 中的 content_for 不呈现任何内容

我该如何解决?

当将其放置在回显的 content_for / yield 标签后面时,它看起来partials无法附加他们的 content_for 内容。content_for :blah do

如果我尝试尝试content_for :scripts_bottom,它将在页面底部呈现。

提前致谢

导轨 3.2.14 红宝石 2.0.0p247

4

3 回答 3

0

而不是provide,尝试<%= content_for :scripts_head %>

于 2013-12-01T00:36:51.050 回答
0

如果你想使用,content_for那么你需要在你的脑海中产生它而不是渲染。

所以你的标题看起来像:

<%= yield :scripts_head %>

或者,您可以删除content_for部分中的 JS,然后像这样单独拥有 JS:

<%= javascript_include_tag 'some_script' %>

这样您就不必更改布局文件。

于 2013-12-01T00:49:06.103 回答
0

在您的layouts/head部分中,使用yield :scripts_head not content_for

于 2013-12-01T00:49:19.253 回答