All threads / jquery doesn't work in js.erb file

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?

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!

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.