1

I have an app where I keep track of different "Games". In the "show"-view, I would like to show the game information plus a select, which I can use to quick-jump to a game.

The quick-jump feature would obviously be some javascript but here's my problem: After some reading, I still don't get how I can have a separate javascript file for each action of a controller. I could add the code to the application.js but I think that would blow-up that file unnecessarily. Any link to a good basic guide for the interaction between Rails 3 and Javascript would also help very much. Thanks!

ndee
  • 225
  • 3
  • 13

2 Answers2

2

The answer to the following question spells this out a bit more specifically and with a helper method to make it a bit more elegant. Same idea though:

Rails 3.1 asset pipeline: how to load controller-specific scripts?

Community
  • 1
  • 1
cailinanne
  • 8,332
  • 5
  • 41
  • 49
1

Just introduce separate javascript files per controller+action basis (e.g. CONTROLLERNAME_ACTIONNAME.js, replacing CONTROLLERNAME and ACTIONNAME with your actual names ;) ) and include them in your views with javascript_include_tag(CONTROLLERNAME_ACTIONNAME.js).

wanderfalke
  • 878
  • 8
  • 8
  • Thanks, that's what I did. I'm sure it's not the perfect Rails-way yet, but I might be slowly getting there. – ndee Aug 19 '11 at 10:11
  • I wouldn't use separate javascript files, since they all need to be cached separately too. You wrote it could blow up that file unnecessarily... about what sizes are we taking here? – Rene van Lieshout Aug 19 '11 at 13:08
  • the sizes are pretty small < 5K but it's still better for the "readability" I would say. – ndee Aug 22 '11 at 06:55