Rails 4 jQuery,javascript和咖啡脚本无法正常工作

我是rails和web开发的新手,尽管我在控制系统和固件中有近二十年的C / C ++,而且还有很多shell和perl脚本。

如果没有明确地包含它,我就无法让jquery工作,即使它在application.js清单中,我也无法让任何单独的coffeescripts工作。

Ubuntu 14.04LTS,ruby 2.2.1p85,rails 4.2.0

的application.js

//= require jquery //= require jquery_ujs //= require turbolinks //= require_tree . 

application.html.erb

     

_header.html.erb

   My Application Title  true %>    true %>      ... 

的Gemfile

 source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.0' # Use sqlite3 as the database for Active Record #gem 'sqlite3' # Use postgresql as the database for Active Record gem 'pg' # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .coffee assets and views gem 'coffee-rails', '~> 4.1.0' # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc # Pagination gem gem 'kaminari' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server # gem 'unicorn' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' # Access an IRB console on exception pages or by using  in views gem 'web-console', '~> 2.0' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end 

配置/ application.rb中

 require File.expand_path('../boot', __FILE__) require 'rails/all' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) module Boe class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # config.time_zone = 'Central Time (US & Canada)' # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de # Do not swallow errors in after_commit/after_rollback callbacks. config.active_record.raise_in_transactional_callbacks = true end end 

我有11个控制器和27个内置的视图,除了我昨天没试过任何javascript之外它们都运行良好。 我没有得到任何预期的行为,所以在我的“人物”索引视图中我添加了:

 ...  $(document).ready(function(){ alert("jQuery is running!"); });  ... 

检查并查看jQuery是否正常工作。 如果我在csrf_meta_tag之前添加 ,我会收到警告消息,但是coffeescript仍然无效。 没有明确的jquery包含,我什么也得不到。

我看过很多很多post,但都没有奏效。 在我看来,application.js中的清单没有被读取和/或列出的模块没有被包括在内,除了在页面之间移动时,turbolink似乎可以通过浏览器上的网络面板正确地进行测量。 我删除了turbolink,它的表现非常不同。

我也尝试过包含jquery-turbolinks gem,尽管我很确定jquery-rails gem不需要它。 但它也无法与未安装的turbolinks一起使用,因此我认为这不是冲突。

谢谢你的期待。

为了解决这个问题,我创建了一个空白的应用程序,只有一个’welcome’控制器,只包含jQuery测试脚本,工作正常。

从裸体应用程序中树的差异向后工作,我发现另一个开发人员生成了一个空的coffeescript文件:

 app/assets/javascript/application.coffee 

我删除了这个文件,应用程序正常工作。 股票javascript_include_tag现在读取清单并包含各种javascript模块,包括jQuery,jQuery-ujs和turbolinks。

Rails 4自动将sass-rails,coffee-rails和uglifier gems添加到您的Gemfile中,Sprockets用它来进行资产压缩。 因此无需明确添加gem。

您不需要使用javascript_include_tag添加jquery,而是包含您的应用程序文件,并让资产管道执行其操作。 类似于:<%= javascript_include_tag“application”%>

尝试添加到config / application.rb

 config.assets.enabled = true 

您可能还需要:

 config.assets.initialize_on_precompile = true 

在本地启动应用程序。 在任何浏览器中打开您的应用 打开页面的源代码。 检查以下行是否存在

   

如果你能看到如下内容

 /*! * jQuery JavaScript Library v1.11.1 * http://jquery.com/ * * Includes Sizzle.js * http://sizzlejs.com/ .......... .......... soon .... 

然后jQuery适用于您的应用程序