Say I need to call a javascript file in the of an ERb template.
My instinct is to do the usual:
<%= javascript_incl
I usually have the following in the layout file:
<head>
<%= javascript_include_tag :defaults %> <!-- For example -->
<%= @extra_head_content %>
</head>
And then in the views:
<% (@extra_head_content ||= "") += capture do %>
<%= other_content %>
<% end %>
See the API documentation for #capture
I would suggest not to add javascript in the header as it causes to load the page slower. Rather load the js at the bottom of the page, which is faster. http://developer.yahoo.com/performance/rules.html#js_bottom
<body>
....
<%= yield(:js) -%>
</body>
And send it there from a view:
<%- content_for(:js) do -%>
<%= javascript_include_tag :defaults -%>
<%- end -%>
I feel there's nothing wrong including all yr defaults since they can then be cached on user's browser.
I would use content_for.
For instance, specify the place to insert it in the application layout:
<head>
<title>Merry Christmas!</title>
<%= yield(:head) -%>
</head>
And send it there from a view:
<%- content_for(:head) do -%>
<%= javascript_include_tag :defaults -%>
<%- end -%>