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' %>
1 Answer 1
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";
pack_tagis used for webpacker. If you are just trying to link to the file you can put it inside your public directory and then just link to the path/folder_name/file_name.jssomething like that. So, for example/public/javascripts/bootstrap.jsand then (I believe) in your view file<script src="/javascripts/bootstrap.js"></script>