ruby on rails - add javascript

12,719

Solution 1

Adding your custom javascript to your rails app is real easy, just follow the steps below :-

  • create a file (say custom.js) under app/assets/javascripts/
  • 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.

Solution 2

For Rails 6

Add your javascript code under /app/javascript//file.js

and then in app/packs/application.js

require("<folder name>/file")

Share:
12,719
user2771150
Author by

user2771150

Updated on July 24, 2022

Comments

  • user2771150
    user2771150 almost 2 years

    I'm a newbie ruby on rails developer. I've just created my first application and made use of the the bootstrap carousel. Now, I want to add some javascript. My application does not have any custom javascript at this point. What I want to do is when the left or right button on the carousel is pressed, I want an alert to pop up. Preferably, I want to include my javascript file in the /assets/javascript directory. The purpose of this is to just learn how to add new javascript files in my application.