Ask A Question

Notifications

You’re not receiving notifications from this thread.

jquery doesn't work in js.erb file

Christopher asked in Rails

Hey I'm working on an rails 6 application and try to render an partial in an js.erb file but get this error in the chrome console:

Uncaught TypeError: Cannot read property 'append' of null

Is there something new with js.erb files in rails 6 or am I missing something?

Reply

Ah I got it I had to add this to my application.js

import $ from 'jquery';
global.$ = jQuery;
Reply

Hello! I'm trying to also get jQuery to work in a create.js.erb file. Would I still need to add the code snippet you provided above if I have this in my environment.js:

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

or should I include it anyway? Thanks!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,796+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.