Loading Javascript per controller in rails
javascript, ruby-on-rails
Solution
As pointed out by `Dave` you can use the proposed solution to his linked question, i.e. by doing:
<%= javascript_include_tag params[:controller] %>
The only thing you'd have to remember to do this is to name your javascript assets the names of the controllers - so `home.js` and `sessions.js` in your case (if I remember Rails' naming conventions correctly).
I have seen other ways of doing this though, which is useful if you want to include some javascript on pages associated with different controllers for whatever reason. This answer, I think, gives a very elegant solution.
First off, add the global javascripts to your manifest file, and include that 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 would load specific javascripts simply add the following:
<% content_for :javascripts do %>
<%= javascript_include_tag 'your_script' %>
<% end %>
Problem
JavaScript file per view in Rails I looked into similar threads, but i am not able to catch. Below is my application.js file. ``` //= require jquery //= require jquery_ujs //= require bootstrap //= require vendor //= require_tree . ``` I have a directory structure of my assets pipeline as below. ``` --javascripts -misc // directory helper.js session.js app.js application.js home.js ``` How would i load the misc directory for all pages and home.js for `HomeController` and session.js for `SessionController`. I don't want unwanted JS to get loaded every where.