rails best practices where to place unobtrusive javascript

I do not think there is one best practice, but I’ll let you know what I do.

  1. I have a series of js files each for with their own purpose in the public/javascripts/ directory. Some examples could be utility.js chat.js shopping_basket.js and so on.

  2. 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)

  3. I allow basic $(document).ready hooks inline in the pages, and keep them really short.

  4. 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)

  5. 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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)