I have a project where I need to use jQuery for my javascript file. I get an error telling me 'Reference error: $ is not defined.' when I load my page from a line of code
$(window).load(function()
The full function is in the play.html.erb snippet below. I believe this is a jQuery error.
I am using Ruby 2.1.2p95 and Rails 4.1.6.
I have done some research on this as to how to get jQuery to work but nothing has helped. I looked at Installing jQuery in Rails 3.2.13
I have tried putting <%= javascript_include_tag "//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" %> in my html.erb file that includes the javascript file I am trying to use the jquery in
I have tried putting <%= javascript_include_tag :application %> in there as well.
There were suggestions to putting the entire file in my app/assets/javascript so I downloaded jquery-2.1.1.min,js and put that in there, and put the <%= javascript_include_tag "jquery-2.1.1.min" %> in the html.erb file and put Rails.application.config.assets.precompile += %w( jquery-2.1.1.min.js ) in my assets.rb and precompiled my assets through the terminal.
I have put the gem 'jquery-rails' in my gemfile.
I have also tried the 2 directions on https://rubygems.org/gems/jquery-rails
I have the normally specified requires in my application.js
I run bundle install after everything I tried just in case.
play.js
$(window).load(function()
{
var c = document.getElementById("charhealthbar");
var ctx = c.getContext("2d");
var health = 5;
ctx.fillStyle = "#FF0000";
ctx.fillRect(0, 0, (health / 100) * 140, 25);
});
assets.rb
Rails.application.config.assets.version = '1.0'
Rails.application.config.assets.precompile += %w( creation.css )
Rails.application.config.assets.precompile += %w( Main.css )
Rails.application.config.assets.precompile += %w( creation.js )
Rails.application.config.assets.precompile += %w( play.css)
Rails.application.config.assets.precompile += %w( play.js )
Rails.application.config.assets.precompile += %w( jquery-2.1.1.min.js )
play.html.erb
<%= stylesheet_link_tag "Main.css" %>
<%= stylesheet_link_tag "play.css" %>
<%= javascript_include_tag "play" %>
<%= javascript_include_tag :application %>
<%= javascript_include_tag "jquery-2.1.1.min" %>
application.js
//= require jquery
//= require jquery_ujs
//= require creation
//= require_tree .
Gemfile
source 'https://rubygems.org'
gem 'rails', '4.1.6'
gem 'pg'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development
gem 'jquery-rails'
I also currently still have the jquery-2.1.1.min.js in my app/assets/javascripts folder. Any help on fixing this error would be greatly appreciated.