【问题标题】:Uncaught ReferenceError: jQuery is not defined in console for Rails 5未捕获的 ReferenceError:Rails 5 的控制台中未定义 jQuery
【发布时间】:2017-09-16 00:42:47
【问题描述】:

我刚刚为我的 Ruby on Rails 应用程序安装了 jQuery UI。它运行 Rails 5。我查看了所有类似的帖子,但它们不适用于我的情况。

我的页面渲染得很好,但是在 Chrome 开发工具控制台中,我收到了这个错误:

我以各种方式重新排列了 application.js 文件,但仍然出现相同的错误。这是文件:

// 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

【问题讨论】:

  • 如果你把//= require jquery放在其他请求之前呢?
  • @maxpleaner 是的,这就是原因

标签: javascript jquery ruby-on-rails ruby


【解决方案1】:

根据上面的评论,在顶部尝试 require jquery。

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

【讨论】:

    【解决方案2】:

    jquery-ui 要求您预先加载默认的 JQuery。这就是您看到所有这些错误的原因,因为jquery-ui 在其整个代码中都引用了它,因此它会抛出异常。你的 application.js 应该是这样的:

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

    【讨论】:

      猜你喜欢
      • 2020-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2021-04-24
      相关资源
      最近更新 更多