Include a specific JavaScript file for a given view

Posted . Visible to the public.

Add <%= yield :javascript_includes %> to application.html.erb:

<%= javascript_include_tag "application" %>
<%= yield :javascript_includes %>

Add to top of your view.html.erb:

<% content_for :javascript_includes do %>
  <%= javascript_include_tag "forms.js" %>
<% end %>

Notes:

  • forms.js should be located in the app/assets/javascripts directory.
  • To avoid duplicate inclussion of js files, you must disable the require_tree directive in application.js. You just have to remove the equal sign:
// require_tree .

More info about this subject:

Profile picture of Saul
Saul
Last edit
Keywords
rails, assets, pipeline
Posted by Saul to rails (2013-01-08 23:42)