I have a file.js.erb
file in my views, but I need load this file in <head></head>
layout.
Is it possible load these files in <head></head>
section?
and if it's possible, How can I do it?
Thanks!
I have a file.js.erb
file in my views, but I need load this file in <head></head>
layout.
Is it possible load these files in <head></head>
section?
and if it's possible, How can I do it?
Thanks!
在您的布局中:
<head>
<script><%=render :file => "layouts/file.js"%></script>
</head>
如果文件包含脚本标签,那么您在布局中不需要它们,并将路径中的布局更改为文件实际所在的位置。
如果您不想在每个页面上都使用它,那么您可以使用 content_for 和 yield (正如 MyYorshiji 在评论中建议的那样)
<head>
<script><%= yield :extra_js %> </script>
</head>
在您希望加载此 js 的视图 html.erb 中
<%content_for :extra_js do %>
<%=render :file => "layouts/file.js"%>
<% end %>
http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for