How to manage assets in Rails 3.1?

OK, so I'm starting a new project using Rails 3.1, and I'm new to CoffeeScript.

In any case, I like the idea of ​​having asset files representing the controllers, but what if I want JS to be displayed only when the controller is called?

For example, I have a controller named Game . In my games.js.coffee file I put some code there, and it called for each page request. Even pages that have nothing to do with Games .

In Rails 3.0.7 , what I would like to do is put yield(:js) in the application erb file, and then call content_for(:js) in my Games#action view. Thus, only js was loaded, which was necessary for this controller.

Or am I mistaken about this? Is it better to download and cache ALL js code for each page request to improve performance?

Thanks for any suggestions.

+4
source share
2 answers

Is it better to download and cache ALL js code for each page request to improve performance?

Basically, the Rails team decided that the answer is usually yes. Most sites work best with a single (mini) JS file, which you get by default in Rails 3.1. Thus, as soon as the user accesses one page of your site, all other pages will load quickly, since all JS are cached.

If your site has a huge amount of JS (I think Facebook), then I suggest you archive your site to download the rarely used JS code asynchronously using a library such as RequireJS . Otherwise, I would not load another code under different controllers; this is most of the extra work for the ultimate benefit.

+5
source

Take a look at this plugin, I think it solves your problem: https://github.com/snitko/specific_assets

0
source

All Articles