Javascript Include Tag Best Practice in a Rails Application Javascript Include Tag Best Practice in a Rails Application ruby-on-rails ruby-on-rails

Javascript Include Tag Best Practice in a Rails Application


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 -%>


I feel there's nothing wrong including all yr defaults since they can then be cached on user's browser.


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 -%>