Reputation: 815
How to use js.erb feature of rails in Rails 6 version?
With rails 6 comes with webpacker, we need to add Jquery as custom option. Is there any way to make js.erb working.
I have jquery working with below setting
const { environment } = require('@rails/webpacker')
const webpack = require("webpack")
environment.plugins.append("Provide", new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
Popper: ['popper.js', 'default'],
}))
module.exports = environment
Even though js.erb is not working.
Upvotes: 1
Views: 1072
Reputation: 101831
In order to use jQuery in your JS.erb files you need to provide jQuery as a global.
Install jQuery with Yarn or NPM:
$ yarn add jquery
Make sure you have the webpack dev server running:
$ bin/webpack-dev-server
Require jQuery in your app/javascript/packs/application.js
:
global.jQuery, global.$ = require("jquery");
Upvotes: 3