I do not think there is one best practice, but I’ll let you know what I do.
-
I have a series of js files each for with their own purpose in the
public/javascripts/
directory. Some examples could beutility.js
chat.js
shopping_basket.js
and so on. -
I use asset packager and define one big fat collection for all my general use functionality and another for admin only functionality. Round trips to the server cost way too much. I basically include all the js in on first page load minified into one blob (in general)
-
I allow basic
$(document).ready
hooks inline in the pages, and keep them really short. -
Data that my js files needs to access is rendered inline with the page. (Usually in the DOM, sometimes as vars – Eg.
var xyz = 100
) -
I will usually develop my controllers with javascript off (and make sure it all works), then I turn it on and sprinkle a few
if request.xhr?
where needed.
Keep in mind, Rail 3.1 introduces a built-in best practice, see: http://guides.rubyonrails.org/asset_pipeline.html – on a personal note I have had performance and configuration issues with the new pipeline, however many others have had great success with it.