4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails6のjs.erb内でJQueryを使う方法

Last updated at Posted at 2020-10-22

ちょっとはまったのでメモ

概要

Rails6のビューのremote:trueなフォームを受けとったコントローラで、js.erb を呼び出した時、JQuery $('#hoge')が動作しなかった。
※webpacker配下では正しく動作済み。

発生したJSエラー: Uncaught ReferenceError: $ is not defined

解決方法

app/javascript/packs/application.js内に以下の一行を追記する

app/javascript/packs/application.js
...
require("bootstrap")
require("admin-lte")

window.$ = jQuery; // ここ!
hogehoge.js.erb
$('.modal_area').html('<%= j(render "modal") %>');
$('#modal').modal('show');

config/webpack/environment.js で同じような定義をしてたけど、js.erb ファイル内から使うにはこの記述が必要だった。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?