[英]Ruby on Rails project not recognizing jQuery
I have a project where I need to use jQuery for my javascript file. 我有一个项目,我需要在我的JavaScript文件中使用jQuery。 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. 完整功能位于下面的play.html.erb片段中。 I believe this is a jQuery error.
我相信这是一个jQuery错误。
I am using Ruby 2.1.2p95 and Rails 4.1.6. 我正在使用Ruby 2.1.2p95和Rails 4.1.6。
I have done some research on this as to how to get jQuery to work but nothing has helped. 我已经就如何使jQuery工作进行了一些研究,但是没有任何帮助。 I looked at Installing jQuery in Rails 3.2.13
我看着在Rails 3.2.13中安装jQuery
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 我尝试将
<%= javascript_include_tag "//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" %>
放入我的html.erb文件中,该文件包含我要尝试的javascript文件使用中的jQuery
I have tried putting <%= javascript_include_tag :application %>
in there as well. 我也尝试将
<%= javascript_include_tag :application %>
放入其中。
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. 有建议将整个文件放入我的app / assets / javascript中,因此我下载了jquery-2.1.1.min,js并将其放在其中,然后将
<%= javascript_include_tag "jquery-2.1.1.min" %>
在html.erb文件中,然后将Rails.application.config.assets.precompile += %w( jquery-2.1.1.min.js )
放入我的asset.rb中,并通过终端对我的资产进行预编译。
I have put the gem 'jquery-rails'
in my gemfile. 我已将gem'jquery
gem 'jquery-rails'
放入我的gemfile中。
I have also tried the 2 directions on https://rubygems.org/gems/jquery-rails 我也在https://rubygems.org/gems/jquery-rails上尝试了2个方向
I have the normally specified requires in my application.js 我的application.js中有通常指定的要求
I run bundle install
after everything I tried just in case. 为了以防万一,我尝试一切后运行
bundle install
。
play.js 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 asset.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 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 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. 目前,我的app / assets / javascripts文件夹中仍然还有jquery-2.1.1.min.js。 Any help on fixing this error would be greatly appreciated.
修复此错误的任何帮助将不胜感激。
You should put the include tag for "play"
after the include tag for jquery
: 您应该将
jquery
的include标记放在"play"
的include标记之后 :
<%= stylesheet_link_tag "Main.css" %>
<%= stylesheet_link_tag "play.css" %>
<%= javascript_include_tag "jquery-2.1.1.min" %>
<%= javascript_include_tag :application %>
<%= javascript_include_tag "play" %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.