Skip to main content

jquery doesn't work in js.erb file

Rails • Asked by Christopher

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?

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

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

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!

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.