Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Include External JavaScript

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?

like image 517
Daniel Kehoe Avatar asked Sep 21 '12 13:09

Daniel Kehoe


People also ask

How to include js file in Rails?

Copy external JavaScript libraries (such as jQuery plugins) to the vendor/assets/javascripts folder. Let the Rails asset pipeline combine them all in one minimized application. js file. List scripts in the app/assets/javascripts/application.

Can you use Javascript with rails?

If you plan to use Rails with importmap-rails to manage your JavaScript dependencies, there is no need to install Node. js or Yarn. When using import maps, no separate build process is required, just start your server with bin/rails server and you are good to go.

How to add JavaScript to Ruby?

Open up your manifest file i.e application. js (this is the file where you need to include every javascript file you want to load into your rails app, add the script name to it //=require custom. Visit your rails app from browser and check the page source and search for custom. js being loaded or not thats all.


3 Answers

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

like image 55
Daniel Kehoe Avatar answered Oct 08 '22 22:10

Daniel Kehoe


To access Javascript on a single file, javascript_include_tag is the best option.

With that what you can do is too add 'Rails.application.config.assets.precompile += %w( yourfilename.js )' to your 'config/initializers/assets.rb' file.

like image 41
Shehryar Avatar answered Oct 08 '22 21:10

Shehryar


In Haml

= javascript_include_tag "example"

To load the file assets/javascripts/example.js

like image 1
Apurv Agarwal Avatar answered Oct 08 '22 23:10

Apurv Agarwal