1 year ago

#354551

test-img

valcod3r

JQuery $ not global throughout application importmaps Rails 7

I'm receiving odd behavior while using importmaps. I can access jquery within the scope of the application.js file and through js files that I import. But I cannot access jquery in normal rails views with script tags. In certain cases, I must use a script tag to interpolate certain env variables. I don't know what causes jquery to only run in js files not be accessible in my views. How do I enable jquery across the entire application (within views and js files)

views/**

   Uncaught ReferenceError: $ is not defined
            at edit:687:3
    <script>
       console.log($)
    </script>

config/manifest.js

//= link_tree ../../../vendor/javascript .js
//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../builds

application.js

// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails
import 'jquery';
import Alpine from "alpinejs";
import { Turbo } from "@hotwired/turbo-rails"
import './vendor/jquery.validate.min';
window.jQuery = $;
window.$ = $;
window.Alpine = Alpine
Alpine.start()
Turbo.session.drive = false
import "@hotwired/turbo-rails"
import "controllers"
import "./navbar"
import "./stripe_resources"
import "alpine-turbo-drive-adapter"

importmap.rb

# Pin npm packages by running ./bin/importmap

pin "application", preload: true
pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true
pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true
pin_all_from "app/javascript/controllers", under: "controllers"
pin "jquery", to: "https://code.jquery.com/jquery-3.6.0.min.js", preload: true
pin "alpinejs", to: "https://ga.jspm.io/npm:alpinejs@3.9.1/dist/module.esm.js"
pin "alpine-turbo-drive-adapter", to: "https://ga.jspm.io/npm:alpine-turbo-drive-adapter@2.0.0/dist/alpine-turbo-drive-adapter.esm.js"
pin "stripe_account_jq_validation"

javascript

jquery

ruby-on-rails

import-maps

0 Answers

Your Answer

Accepted video resources