Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require custom JS files in Rails 6

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:

  1. 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.

  2. Copy all my js files under app/javascript/channels (which should be included by default, since application.js has require("channels")).

  3. Adding require_tree . to application.js, which was the previous approach.

How can I load my own js files inside a Rails 6 application?

like image 917
Severin Avatar asked May 21 '19 16:05

Severin


People also ask

Where is application js in Rails 6?

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.

How do I add JavaScript to my ERB?

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.

How do I include a file in a JavaScript file?

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.

How to add custom JavaScript file to rails 6 (webpacker)?

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>

What is the latest version of rails for JavaScript?

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.

What is the default compiler for JavaScript in rails 6?

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.

Can I integrate JavaScript into my Rails application?

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.


Video Answer


2 Answers

Get better-organized code and avoid multiple javascript_pack_tags in your application.html.erb file with this approach:

  1. Add your custom example.js javascript file to app/javascript/packs.
  2. Add 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.

like image 108
Oliver Curting Avatar answered Oct 16 '22 20:10

Oliver Curting


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.

like image 23
asimhashmi Avatar answered Oct 16 '22 18:10

asimhashmi