Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby on rails - add javascript

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.

like image 994
user2771150 Avatar asked Dec 06 '15 05:12

user2771150


2 Answers

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.
like image 84
sghosh968 Avatar answered Oct 18 '22 23:10

sghosh968


For Rails 6

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

and then in app/packs/application.js

require("<folder name>/file")

like image 38
Adarsh Rao Avatar answered Oct 19 '22 01:10

Adarsh Rao