12

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.

Community
  • 1
  • 1
theJava
  • 14,620
  • 45
  • 131
  • 172
  • possible duplicate of [Using Rails 3.1, where do you put your "page specific" javascript code?](http://stackoverflow.com/questions/6167805/using-rails-3-1-where-do-you-put-your-page-specific-javascript-code) – Dave Schweisguth May 14 '14 at 12:24

2 Answers2

13

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

Artur Haddad
  • 1,429
  • 2
  • 16
  • 33
nicohvi
  • 2,270
  • 2
  • 28
  • 42
  • I get this error "Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( login.js )` to `config/initializers/assets.rb` and restart your server" – theJava May 15 '14 at 09:21
  • Yeah, if you want to add custom javascripts which are not part of your manifest you need to add them for asset precompilation manually. As stated in the error message you can simply add that line to the initializer, or you could add it to your environment files (production.rb, development.rb etc.) – nicohvi May 15 '14 at 12:17
  • Can you explain me on what is asset precompilation. I have a login.js file, why does it need to get compiled. I am confused here. – theJava May 15 '14 at 12:21
  • 1
    Sure, no worries. It has to be compiled so that it respects Rails' usage of [fingerprinting](http://guides.rubyonrails.org/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care-questionmark) - so that if you change something in `login.js` Rails knows to fetch a new version of the file due to a change in fingerprints. To fully understand asset compilation I **strongly** suggest reading this: http://edgeguides.rubyonrails.org/asset_pipeline.html – nicohvi May 15 '14 at 12:46
3

The problem with the solution above is it requires an additional request for different JS files for every controller. The better solution is to organize all JS so that it can be compiled into a single file with the asset pipeline, and then called based on the current controller and action.

There are a few gems out there that take care of this for you. RailsScript is a simple one that calls JS objects named after the current controller and then calls a method in that object, named after the current action. This make writing and maintaining page specific JavaScript for rails very easy.

https://github.com/gemgento/rails_script

i.e.

# app/assets/javascripts/users.js.coffee

window.App ||= {}
class App.Users extends App.Base

   show: ->
      alert('The users#show action!')

Since RailsScript is also compatible with Turoblinks, your users will only need to load the JS and CSS assets once! Not on every page.

KPheasey
  • 1,765
  • 16
  • 22