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
//= requ
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.