Search code examples
javascriptjqueryruby-on-railscode-organization

how to organize javascripts using rails and jquery


i'm working in a big and rich rails web application using tons of javascript. I would like to know if anybody has a tip to organize the javascripts. Today i'm generating a new file named controller.js and adding it to my views using content_for. The problem is, some files are becoming big and sometimes, i need a function from one controller in another, so then in the end, i add a products.js to a details controller just to keep DRY. Is that solution good? Any other tip? I think the same pattern can be applied as well to css files?


Solution

  • I actually just posted my current approach to organising JS in Ruby on Rails.

    I have a namespaced JavaScript file for each of my controllers and then I use asset_packager to bind these many small files into a single file at deploy time.

    You could make this more granular and have a file for each action in a controller, but it means I have many small files scoped to small elements of functionality in the my system.

    Works really well.