简体   繁体   English

Rails 6:未捕获的引用错误 - $ 未定义

[英]Rails 6: Uncaught Reference Error - $ is not defined

I'm trying to get a semantic UI button to work using Javascript in Rails 6. However, none of my code is working properly.我正在尝试让语义 UI 按钮在 Rails 6 中使用 Javascript 工作。但是,我的代码都没有正常工作。 The code looks like this:代码如下所示:

$(document).on('turbolinks:load', function() {
   $('.message .close').on('click', function() {
   $(this).closest('.message').transition('fade');
   })
;
})

The error I'm getting is: Uncaught ReferenceError: $ is not defined.我得到的错误是:Uncaught ReferenceError: $ is not defined。

My application JS file looks like:我的应用程序 JS 文件如下所示:

// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
// Loads all Semantic javascripts
//= require jquery
//= require semantic-ui
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .
// Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
// or the `imagePath` JavaScript helper below.
//
// const images = require.context('../images', true)
// const imagePath = (name) => images(name, true)
Uncaught ReferenceError: $ is not defined

This error references that $ is not declared yet which eventually denotes that jquery is not loaded into the page properly此错误引用 $ 尚未声明,最终表示 jquery 未正确加载到页面中

check whether jquery gem is added in the gemfile or not检查gemfile中是否添加了jquery gem

Also, try whether using jQuery as identifier instead of $另外,尝试是否使用jQuery作为标识符而不是$

在您的 html 页面中添加 jquery 支持文件。

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM