As Dave pointed out, you can use the proposed solution for a related question, that is:
<%= javascript_include_tag params[:controller] %>
The only thing you needed to remember was to name your javascript assets with controller names - so home.js and sessions.js in your case (if I remember the Rails naming conventions correctly).
I have seen other ways to do this, although this is useful if you want to include some javascript on pages linked to different controllers for any reason. This answer, I think, gives a very elegant solution.
First of all, add global javascripts to your manifest file and include this in your application.html.erb layout file.
<html> <head> # stuff <%= javascript_include_tag 'application' %> <%= yield :javascripts %> </head> <body> <%= yield :content %> </body> </html>
And then in the views where you load certain javascripts, just add the following:
<% content_for :javascripts %> <%= javascript_include_tag 'your_script' %> <% end %>
nicohvi
source share