how to add js file in rails 6
rails public folder
Is this the right way? If yes, is it possible for me to follow the same steps in adding jQuery and Bootstrap library?
Any help is appreciated.
The right way to include your files is:
//= require customejsfile
application.js file. What's more, by default you have
//= require_tree .
which requires all js files from
Taking a look at the application.html.erb file, you'll see that the application.js was referenced via:
Hope this helps.
The Asset Pipeline — Ruby on Rails Guides, +1 for Bower, it's actually pretty straightforward to introduce in a Rails you add it to your rails app if you simply wanted to link to the js file via� Getting Started with RailsThis guide covers getting up and running with Ruby on Rails.After reading this guide, you will know: How to install Rails, create a new Rails application, and connect your application to a database. The general layout of a Rails application. The basic principles of MVC (Model, View, Controller) and RESTful design. How to quickly generate the starting pieces of a Rails
You have to explicitly add jquery and bootstrap in application.js, whereas you will automatically include custom files requiring the directory tree. If you take a look at your application.js there should be a manifest saying that. This is how your application.js should look like with bootstrap and jquery
No need to add your custom file in this list as you are requiring all the files at the same level of your application.js (or lower in the directory tree) with the last directive (
require tree .)
<script src="<%= asset_path 'my_js_file' %>"></script>
- You don't need to precise the JS extension, sprockets supposes them to be js.
- @MarekLipka hi thank you Marek, would you know what to do if you don't have the actual .js file but have a link to a CDN - how would one incorporate it in rails?
- I would probably put the link in html, like you do normally in html sites.