2

レイアウトを使用するレールアプリがあります

簡易版は次のようになります。

<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 を単独で使用することもできます。

<%= 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 に答える