0
Help us understand the problem. What are the problem?

posted at

Uncaught ReferenceError: $ is not defined Rails6

Rails6で非同期処理がうまくいかなかったので備忘録。

Uncaught ReferenceError: $ is not defined

原因としては

・jQueryがうまく読み込めていなかった。
・Rails6でのjQuery導入ができていなかった

:writing_hand:参考にした記事

導入方法

$ yarn add jquery

config/webpack/environment.js
javascript/packs/application.js
に下記を追加

environment.js
const { environment } = require('@rails/webpacker')

//追加部分 
const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)

module.exports = environment

application.js
require("jquery")

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
0
Help us understand the problem. What are the problem?