Pierre P.
Pierre P.

Reputation: 1065

jQuery not loaded on my rails application (v 5.1.2)

It seems that jQuery is not loaded in my rails application, according to this test: https://stackoverflow.com/a/7341884/6391764.

The output I've got is Doesn't work

I'm running my rails application on a Debian virtual machine on top of my Mac.

Here's my :

Gemfile

source 'https://rubygems.org'

gem 'rails' # Well ...
gem 'puma'
gem 'sass-rails'
gem 'uglifier'
gem 'coffee-rails'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder'

group :development, :test do
    gem 'sqlite3'
    gem 'byebug'
end

group :development do
    gem 'web-console'
    gem 'listen'
    gem 'spring'
    gem 'spring-watcher-listen'
end

group :test do
    gem 'rails-controller-testing'
    gem 'minitest-reporters'
    gem 'guard'
    gem 'guard-minitest'
end

group :production do
    gem 'pg'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

I've run both bundle install and bundle update.

My 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 rails-ujs
//= require turbolinks
//= require_tree .
//= require_self

window.onload = function() {
    if (window.jQuery) {
        // jQuery is loaded
        alert("Yeah!");
    } else {
        // jQuery is not loaded
        alert("Doesn't Work");
    }
}

My application.html.erb

<!DOCTYPE html>
<html>

    <head>
        <title>Title</title>
        <%= csrf_meta_tags %>
        <%= stylesheet_link_tag    'application', media: 'all',
                                              'data-turbolinks-track': 'reload' %>
        <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
    </head>

    <body>


        <%= render 'layouts/header' %>

        <%= yield %>

        <%= render 'layouts/footer' %>


    </body>
</html>

Upvotes: 0

Views: 98

Answers (1)

vcsjones
vcsjones

Reputation: 141638

You need to add this to your application.js, likely above the require_tree . line:

//= require jquery

Upvotes: 3

Related Questions