For site wide specific JS code (i.e. for the header, which appears on all pages). Where should this be placed? In:
app/assets/javascripts/application.js
Is that right?
For pages#home. Which root_url also points to (root :to => 'pages#home'). Where should my JS file be placed in the pipeline?
app/assets/javascripts/pages/home.js
And regards to my application.js. Is this right? It currently looks like:
//= require jquery
//= require jquery_ujs
//= require_directory .
Or should I just embed page specific JS in the view?
It’s not a good idea to put js code in application.js. You can put that code in any other file on the
assets/javascriptsfolder and it will be included automatically by therequire_directoryorrequire_treecommand. Yourapplication.jsfile is perfectly fine as it is right now, but you might want to userequire_treeinstead ofrequire_directoryfor recursive inclusion.For example, the javascripts files in
app/assets/javascripts/pageswill be included byrequire_treebut not byrequire_directory.Recommended reading: Asset pipeline guide