What Is The Best Way To Include jQuery In A Rails App?
Rules of thumb
- Allow the asset pipeline of Rails to combine all of it in a smaller application.js file.
Principles of performance
- Downloading files will take a lot more time than parsing and then executing browser code. Today’s web browsers can download files (CSS files, scripts, or images) in parallel. This is because modern web browsers have to cache files to reduce download requests, both within and across sites. Even so, multiple files can be slower than single files, with parallel downloads and caching.
- The lesser script tags you use, the faster your pages will load. Our modern web browsers download scripts in parallel. However, each script tag has to be parsed and evaluated to determine whether a file is cached and current. So, dynamic loading, from within another script, is faster than using an additional script tag.
The standard issue with using jQuery with Rails in an application of higher or moderate complexity is the trade-off between including all of it in a single application.js and having multiple, modular files. This will be an issue for you if you plan to use Rails more than the users of Rails who seek to use the jQuery library as a drop-in replacement by making use of Prototype helpers.
Use include_jquery in your layout
And there, you have it.