2

为什么我会收到此错误?
我可以 在 show.html.erb 中使用javascript_include_tagwith吗? 我想把线加载到里面content_for(:head)
<head>...</head>

如果我放<%= content_for(:head) do %>,加载“refresh_chat.js”的行将不会出现。
如果我删除<%= content_for(:head) do %>,加载“refresh_chat.js”的行出现,但不在<head>

意见/帖子/show.html.erb

1. <%= content_for(:head) do %>
2.   <%= javascript_include_tag "refresh_chat.js" %>
3. <% end %>   
4

3 回答 3

3

它应该工作。确保您yield在以下<head>部分:

<head>
  ...
  <%= yield :head %>
</head>

进而

<% content_for(:head) do %>
  <%= javascript_include_tag "refresh_chat.js" %>
<% end %>
于 2013-01-10T12:56:13.350 回答
3

您可能不想<% content_for ...在部分中输出 content_for 块 ( )。也不content_for(:head)是什么神奇的功能什么的。如果你想把它的内容放到你的脑海中,你必须在你的布局中做这样的事情:

<head>
  ...
  <%= yield(:head) if content_for?(:head) %>
</head>
于 2013-01-10T12:58:27.077 回答
0

您应该只使用<% content_for(:head) do %>而不是<%= content_for(:head) do %>,以便内容不会在您的视图中呈现,而是在您的<head>.

于 2013-01-10T12:53:23.033 回答