简体   繁体   English

在Rails中加载Javascript

[英]Javascript loading in Rails

I have my javascript imports set up like this in my Rails app: 我在Rails应用中设置了如下的javascript导入:

  <script>
    WebFontConfig = {
      google: {
        families: [
          'Open+Sans:300,400:latin'
        ]
      }
    };
  </script>
  <script src="//ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js" />
  <script src="http://maps.googleapis.com/maps/api/js?sensor=false" />
  <%= javascript_include_tag 'application' %>

My application.js.erb file is: 我的application.js.erb文件是:

//= require jquery
//= require jquery_ujs
//= require modernizr.custom.44325

//= require jquery-backstretch/jquery.backstretch
//= require infinite-scroll/jquery.infinitescroll

//= require global

When I run the app, my fonts load but I get these errors shown in the console: 当我运行该应用程序时,会加载我的字体,但会在控制台中显示以下错误:

Uncaught ReferenceError: jQuery is not defined jquery_ujs.js?body=1:400 Uncaught ReferenceError: jQuery is not defined jquery.backstretch.js?body=1:378 Uncaught ReferenceError: jQuery is not defined jquery.infinitescroll.js?body=1:815 Uncaught ReferenceError: $ is not defined 未捕获的ReferenceError:未定义jQuery jquery_ujs.js?body = 1:400未捕获的ReferenceError:未定义jQuery jquery.backstretch.js?body = 1:378未捕获的ReferenceError:未定义jQuery jquery.infinitescroll.js?body = 1 :815未捕获ReferenceError:未定义$

I have tried reordering the imports and depending on the order one of a couple of things happens: 我尝试对导入进行重新排序,并根据顺序发生了几件事:

  1. Fonts load but nothing else, jquery not defined errors. 字体加载,但没有其他,jQuery未定义错误。
  2. Page half loads, with javascript and no fonts. 页面一半加载,使用javascript并且没有字体。

Any ideas on what could be going on? 关于会发生什么的任何想法? I've not had errors like this before? 我以前没有这样的错误吗?

Gemfile: 宝石文件:

source 'https://rubygems.org'

gem 'rails', '3.2.12'
gem 'pg'
gem "flickraw", "0.9.6"
gem 'kaminari', "0.15.0"
gem 'bourbon'
gem 'simple_form'
gem 'sendgrid'
gem 'rdiscount'
gem 'haml'
gem 'slim', '~> 1.3.9'
gem 'naturalsorter', '0.2.2'
gem 'friendly_id'
gem 'awesome_print'
gem 'httparty'
gem 'nokogiri'
gem 'unicorn'
gem 'capistrano'
gem 'mechanize'

group :assets do
  gem 'jquery-rails', '2.2.1'
  gem 'sass', github: 'nex3/sass'
  gem 'sass-rails', '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'uglifier', '>= 1.0.3'
  gem 'susy', '2.0.0.beta.3'
  gem 'compass-rails', '>= 1.1.3'
  gem 'compass', '~> 1.0.0.alpha.17'
end

group :development do
  gem 'better_errors'
  gem 'binding_of_caller'
end

group :test do
  gem 'turn', require: false
  gem 'faker'
  gem 'capybara'
  gem 'database_cleaner'
  gem 'launchy'
  gem 'simplecov'
  gem 'spork-rails'
  gem 'guard-rspec'
  gem 'guard-spork'
end

group :development, :test do
  gem 'rspec-rails'
  gem 'shoulda'
  gem 'factory_girl_rails'
end

Solved this. 解决了这个。 It wasn't liking the self closing script tags. 它不喜欢自动关闭脚本标签。 Changed them to <script> and </script> and it's working perfectly now. 将它们更改为<script></script> ,现在可以正常使用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM