简体   繁体   中英

Uncaught ReferenceError: jQuery is not defined in console for Rails 5

I just installed the jQuery UI for my Ruby on Rails application. It runs Rails 5. I have looked at all the similar posts, but they do not apply to my case.

My page renders just fine, but in the Chrome dev tools console, I get this error:

在此处输入图片说明

I have rearranged the application.js file all kinds of ways and still get the same error. This is the file:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery-ui
//= require rails-ujs
//= require html.sortable
//= require turbolinks
//= require_tree .
//= require popper
//= require bootstrap-sprockets

Per the comment above, try the require jquery at the top.

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require turbolinks
//= require_tree .

jquery-ui requires you to have the default JQuery loaded beforehand. That's the reason you are seeing all those errors, as jquery-ui has references to it throughout its code, hence its throwing exceptions. Your application.js should look like this instead:

//= require jquery
//= require jquery-ui
//= require rails-ujs
//= require html.sortable
//= require turbolinks
//= require_tree .
//= require popper
//= require bootstrap-sprockets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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