I'm currently trying Rails 6.0.0.rc1 which seems to have moved the default javascript
folder from app/assets/javascript
to app/javascript
. The application.js
file is now located in app/javascript/packs
. Now, I want to add a couple of js files, but for some reason they don't get imported and I can't find any documentation on how this can be done in Rails 6. I tried a couple of things:
Create a new folder custom_js
under app/javascript/packs
, putting all my js files there and then add a require "custom_js"
to application.js
.
Copy all my js files under app/javascript/channels
(which should be included by default, since application.js
has require("channels")
).
Adding require_tree .
to application.js
, which was the previous approach.
How can I load my own js files inside a Rails 6 application?
The directory structure for JavaScript has changed to the app/javascript/packs/ folder. In that folder you will find the application. js file, which is just like the application.
you can create a custom js file in /assets/javascripts folder and write all your js code and it will load in your page also.
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file.
You need to do the following steps to add custom javascript file to rails 6 (webpacker) 1 .Create your custom file named custom.js in app/javascript/packs directory. For testing purpose, write any console.log in it. 2. Go to your application.html.erb and add the following line at the end of your <head></head>
At the time of writing, the latest version of the framework is Rails 6, and it has brought some changes to the way you interact with JavaScript. Prior to Rails 6, we had the asset pipeline to manage CSS and JavaScript files. But starting from Rails 6, Webpacker is the default compiler for JavaScript.
But starting from Rails 6, Webpacker is the default compiler for JavaScript. CSS is still managed by the asset pipeline, but you can also use Webpack to compile CSS. You won't find your JavaScript folder in the same place as you did in Rails 5.
This guide covers the options for integrating JavaScript functionality into your Rails application, including the options you have for using external JavaScript packages and how to use Turbo with Rails. How to use Rails without the need for a Node.js, Yarn, or a JavaScript bundler.
Get better-organized code and avoid multiple javascript_pack_tags in your application.html.erb file with this approach:
example.js
javascript file to app/javascript/packs
.require("packs/example")
to your application.js
file.I would have liked to add a comment to Asim Hashmi's correct answer. But I don't have enough reputation, so I'll add my suggestion as an answer instead.
It isn't necessary to include the ".js" extension inside of the require.
You need to do the following steps to add custom javascript file to rails 6 (webpacker)
1.Create your custom file named custom.js
in app/javascript/packs
directory.
For testing purpose, write any console.log
in it.
// app/javascript/packs/custom.js
console.log("custom js file loaded")
2. Go to your application.html.erb
and add the following line at the end of your <head></head>
<%= javascript_pack_tag 'custom', 'data-turbolinks-track': 'reload' %>
3. Now execute rake assets:precompile
This will pack your javascript code (including our custom file we just added)
Now reload your page and you should see the message
custom js file loaded
In your browser console.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With