Rails. Uncaught Referenceerror: jQuery is not defined

Asked

Viewed 702 times

0

I am implementing a part of notifications in a Rails project, from a tutorial and am getting this error.

Uncaught ReferenceError: jQuery is not defined
    at notifications.self-3e6330e4ab7198d8dee61da361c9c6cd1a536fd445b9ce4b6792ea023705ae80.js?body=1:20
    at notifications.self-3e6330e4ab7198d8dee61da361c9c6cd1a536fd445b9ce4b6792ea023705ae80.js?body=1:24

My Notifications.coffee is like this

class Notifications
  constructor: ->
    @notifications = $("[data-behavior='notifications']")
    @setup() if @notifications.length > 0

  setup: ->
    console.log(@notifications)

jQuery ->
  new Notifications

and is generating the following . js

(function() {
  var Notifications;

  Notifications = (function() {
    function Notifications() {
      this.notifications = $("[data-behavior='notifications']");
      if (this.notifications.length > 0) {
        this.setup();
      }
    }

    Notifications.prototype.setup = function() {
      return console.log(this.notifications);
    };

    return Notifications;

  })();

  jQuery(function() { //linha 20
    return new Notifications;
  });

}).call(this); //linha 24

My html like this

  <li class="nav-item btn-group" data-behavior="notifications">
            <a class="dropdown-toggle nav-link" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="">
            Notifications <b class="caret"></b>
            </a>
            <div class="dropdown-menu" aria-labelledby="dropdownMenu1">
            <a class="dropdown-item" href="#">Action</a><br />
            <a class="dropdown-item" href="#">Another action</a><br />
            <a class="dropdown-item" href="#">Something else here</a><br />
            </div>
  </li>

1 answer

1


The library jQuery needs to be the first to be loaded on the page. Something that wasn’t happening when the files were compiled CoffeeScript in JavaScript and served.

So I changed my file application js the part referring to this library, to be included first of all the others, including the Bootstrap.

...
//= require jquery
//= require jquery_ujs
//= require rails-ujs
//= require turbolinks
//= require_tree .
//= require bootstrap

Browser other questions tagged

You are not signed in. Login or sign up in order to post.