How to use JQuery in js.erb of Rails6

I'm a little addicted, so make a note

Overview

JQuery $ ('# hoge') didn't work when I called js.erb on a controller that received the remote: true form of a Rails 6 view.

JS error occurred: ʻUncaught ReferenceError: $ is not defined`

Solution

Add the following line in app / javascript / packs / application.js

app/javascript/packs/application.js


...
require("bootstrap")
require("admin-lte")

window.$ = jQuery; //here!

js:hogehoge.js.erb


$('.modal_area').html('<%= j(render "modal") %>');
$('#modal').modal('show');

I had a similar definition in config / webpack / environment.js, but I needed this description to use it from within the js.erb file.

Recommended Posts

How to use JQuery in js.erb of Rails6
How to introduce jQuery in Rails 6
How to use custom helpers in rails
How to use MySQL in Rails tutorial
How to use credentials.yml.enc introduced in Rails 5.2
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
[Rails] How to use select boxes in Ransack
[Rails] How to use PostgreSQL in Vagrant environment
[Rails] How to use enum
[Rails] How to use enum
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
How to use CommandLineRunner in Spring Batch of Spring Boot
[Rails] How to use gem "devise"
How to use Lombok in Spring
How to deploy jQuery on Rails
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[Rails] How to write in Japanese
How to use setDefaultCloseOperation () of JFrame
[Rails] How to use Active Storage
How to use classes in Java?
[Introduction to Rails] How to use render
How to install Swiper in Rails
How to deploy jQuery in your Rails app using Webpacker
[Rails5] japanMap link How to write parameters in js.erb file
[Rails] Use jQuery
How to implement search functionality in Rails
How to change app name in rails
[Ruby on Rails] How to use CarrierWave
How to use named volume in docker-compose.yml
[Rails] How to use rails console with docker
How to insert a video in Rails
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use Docker in VSCode DevContainer
[rails] How to configure routing in resources
[Java] [Maven3] Summary of how to use Maven3
[Ruby on Rails] How to use redirect_to
How to implement ranking functionality in Rails
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
How to use environment variables in RubyOnRails
[Rails] How to use video_tag to display videos
Understand in 5 minutes !! How to use Docker
[Rails] How to use helper method, confimartion
How to use ExpandableListView in Android Studio
[Sprint Boot] How to use 3 types of SqlParameterSource defined in org.springframework.jdbc.core.namedparam
How to delete large amounts of data in Rails and concerns
How to get boolean value with jQuery in rails simple form
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
Summary of how to select elements in Selenium
[Rails] Introduction of pry-rails ~ How to debug binding.pry
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for