Thursday, April 25, 2024
180
rated 0 times [  187] [ 7]  / answers: 1 / hits: 18018  / 12 Years ago, fri, september 21, 2012, 12:00:00

I want to use a JavaScript library such as a jQuery plugin. Do I use the Rails asset pipeline? Or should I include it with a javascript_include_tag? What are my options and what is the recommended practice?


More From » ruby-on-rails

 Answers
-3

Will you use the JavaScript library on only a few pages or throughout the application? If you will use it throughout the application, use the asset pipeline by adding it to the vendor/assets/javascripts folder. If you plan to use the library on a single page, use the javascript_include_tag.



Here are rules of thumb to guide your use of JavaScript in Rails:




  • Logically organize your site-wide scripts in the app/assets/javascripts/ folder.


  • Copy external JavaScript libraries (such as jQuery plugins) to the vendor/assets/javascripts folder.


  • List site-wide scripts in the app/assets/javascripts/application.js manifest.


  • Let the Rails asset pipeline combine them all in one minimized application.js file.


  • For scripts that are used on a few pages that have few visits, load as page-specific JavaScript.


  • Put page-specific JavaScript in the lib/assets/javascripts folder.




For page-specific JavaScript, use <%= yield(:head) %> in the application layout and <% content_for :head ... %> in the view.



For a full explanation with all the details, see my article:




Including External JavaScript Files in Rails



[#82970] Thursday, September 20, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
campbelljonahb

Total Points: 247
Total Questions: 97
Total Answers: 110

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
;