nourza
nourza

Reputation: 2321

Javascript file is not working on my ruby on rails app

How can I call javascript file in my ruby on rails website. For example I have this file and its inside projectname/app/assets/javascripts/bootstrap.js

Here what I tried and all of them are wrong

 <script src="~/app/assets/javascripts/bootstrap.js"></script>
  <script src="/assets/javascripts/bootstrap.js"></script>
  <script src="../javascripts/bootstrap.js"></script>

I got this error

(index):607 GET http://localhost:3000/~/app/assets/javascripts/bootstrap.js net::ERR_ABORTED 404 (Not Found)

in my application.html.erb

<%= javascript_pack_tag 'application' %>

Upvotes: 0

Views: 631

Answers (1)

Hackman
Hackman

Reputation: 1719

Since you are using webpack, you need to import all the JS files into your application.js

In your application.html.erb you got:

<%= javascript_pack_tag 'application' %>

So it is expecting that you have application.js inside app/javascript/packs

Inside that file you can include the bootstrap.js

So you can make a dir: app/javascript/bootstrap and place your bootstrap.js inside that folder. Than you can include it inside your application.js

# app/javascript/packs/application.js
import "../bootstrap/bootstrap";

Upvotes: 2

Related Questions