Rails. Uncaught ReferenceError: jQuery is not defined

0

I'm implementing a portion of notifications in a rails project, from a tutorial and I'm 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 so

  <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>
    
asked by anonymous 31.08.2017 / 17:46

1 answer

1

The jQuery library must be the first to be loaded on the page. Something that was not happening when the CoffeeScript files were compiled into JavaScript and served.

So, I changed the part for this library in application.js to be included first of all, including Bootstrap .

...
//= require jquery
//= require jquery_ujs
//= require rails-ujs
//= require turbolinks
//= require_tree .
//= require bootstrap
    
01.09.2017 / 15:00