Rails 4 jQuery、javascript 和咖啡脚本不工作
Rails 4 jQuery, javascript and coffee scripts not working
我是 rails 和 web 开发的新手,尽管我在控制系统和固件方面有将近二十年的 C/C++ 经验,并且有相当多的 shell 和 perl脚本。
如果不明确包含它,我无法让 jquery 工作,即使它在 application.js 清单中,我也无法让任何单独的 coffeescripts 工作.
Ubuntu14.04LTS,ruby2.2.1p85,rails4.2.0
application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
application.html.erb
<%= render "layouts/header" %>
<%= render "layouts/sidenav" %>
<%= yield %>
<%= render "layouts/footer" %>
_header.html.erb
<html>
<head>
<title>My Application Title</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'intranet' %>
<%= stylesheet_link_tag 'form' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="shortcut icon" href="favicon.ico" />
</head>
<body>
...
宝石文件
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 <%= console %> 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
config/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
我构建了 27 个控制器和视图中的 11 个,它们都运行良好,只是我直到昨天才尝试 javascript。我没有得到任何预期的行为,所以在我的 "people" 索引视图中我添加了:
...
<script>
$(document).ready(function(){
alert("jQuery is running!");
});
</script>
...
检查 jQuery 是否正常工作。如果我在 csrf_meta_tag 之前添加 <%= javascript_include_tag 'jquery.js' %>
,我会收到警告消息,但 coffeescript 和其余的仍然不起作用。没有明确的 jquery 包含,我什么也得不到。
我看过很多很多关于此的帖子,但是 none 其中有效果。在我看来 application.js 中的清单未被读取 and/or 列出的模块不包括在内,除了 turbolink 在从一个页面移动到另一个页面时似乎可以通过浏览器上的网络面板正常工作.我删除了 turbolink,它的行为非常不同。
我也尝试包括 jquery-turbolinks gem,尽管我很确定 jquery-rails [=56] 不需要它=].但它也不适用于卸载 turbolinks,所以我认为这不是冲突。
感谢观看。
Rails 4 自动添加 sass-rails, coffee-rails 和 uglifier gems 到你的 Gemfile, Sprockets 使用用于资产压缩。因此无需显式添加 gem。
您不需要添加 jquery 和 javascript_include_tag,而是包含您的应用程序文件,让资产管道完成它的工作。类似于:<%= javascript_include_tag "application" %>
尝试添加到 config/application.rb
config.assets.enabled = true
您可能还想要:
config.assets.initialize_on_precompile = true
在本地启动您的应用程序。在任何浏览器中打开您的应用程序。打开页面的源代码。检查下一行是否存在
<script src="/assets/jquery.js?body=1" data-turbolinks-track="true">
如果您能看到如下内容
/*!
* jQuery JavaScript Library v1.11.1
* http://jquery.com/
*
* Includes Sizzle.js
* http://sizzlejs.com/
..........
..........
soon ....
然后 jQuery 可以处理您的应用程序
为了解决这个问题,我创建了一个只有 'welcome' 控制器的空白应用程序,其中仅包含 jQuery 测试脚本,效果很好。
从裸应用程序树的差异向后工作,我发现另一个开发人员生成了一个空的 coffeescript 文件:
app/assets/javascript/application.coffee
我删除了这个文件,应用程序正常运行。库存 javascript_include_tag
现在读取清单并包含各种 javascript 模块,包括 jQuery、jQuery-ujs 和 turbolinks。
我是 rails 和 web 开发的新手,尽管我在控制系统和固件方面有将近二十年的 C/C++ 经验,并且有相当多的 shell 和 perl脚本。
如果不明确包含它,我无法让 jquery 工作,即使它在 application.js 清单中,我也无法让任何单独的 coffeescripts 工作.
Ubuntu14.04LTS,ruby2.2.1p85,rails4.2.0
application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
application.html.erb
<%= render "layouts/header" %>
<%= render "layouts/sidenav" %>
<%= yield %>
<%= render "layouts/footer" %>
_header.html.erb
<html>
<head>
<title>My Application Title</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'intranet' %>
<%= stylesheet_link_tag 'form' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="shortcut icon" href="favicon.ico" />
</head>
<body>
...
宝石文件
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 <%= console %> 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
config/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
我构建了 27 个控制器和视图中的 11 个,它们都运行良好,只是我直到昨天才尝试 javascript。我没有得到任何预期的行为,所以在我的 "people" 索引视图中我添加了:
...
<script>
$(document).ready(function(){
alert("jQuery is running!");
});
</script>
...
检查 jQuery 是否正常工作。如果我在 csrf_meta_tag 之前添加 <%= javascript_include_tag 'jquery.js' %>
,我会收到警告消息,但 coffeescript 和其余的仍然不起作用。没有明确的 jquery 包含,我什么也得不到。
我看过很多很多关于此的帖子,但是 none 其中有效果。在我看来 application.js 中的清单未被读取 and/or 列出的模块不包括在内,除了 turbolink 在从一个页面移动到另一个页面时似乎可以通过浏览器上的网络面板正常工作.我删除了 turbolink,它的行为非常不同。
我也尝试包括 jquery-turbolinks gem,尽管我很确定 jquery-rails [=56] 不需要它=].但它也不适用于卸载 turbolinks,所以我认为这不是冲突。
感谢观看。
Rails 4 自动添加 sass-rails, coffee-rails 和 uglifier gems 到你的 Gemfile, Sprockets 使用用于资产压缩。因此无需显式添加 gem。
您不需要添加 jquery 和 javascript_include_tag,而是包含您的应用程序文件,让资产管道完成它的工作。类似于:<%= javascript_include_tag "application" %>
尝试添加到 config/application.rb
config.assets.enabled = true
您可能还想要:
config.assets.initialize_on_precompile = true
在本地启动您的应用程序。在任何浏览器中打开您的应用程序。打开页面的源代码。检查下一行是否存在
<script src="/assets/jquery.js?body=1" data-turbolinks-track="true">
如果您能看到如下内容
/*!
* jQuery JavaScript Library v1.11.1
* http://jquery.com/
*
* Includes Sizzle.js
* http://sizzlejs.com/
..........
..........
soon ....
然后 jQuery 可以处理您的应用程序
为了解决这个问题,我创建了一个只有 'welcome' 控制器的空白应用程序,其中仅包含 jQuery 测试脚本,效果很好。
从裸应用程序树的差异向后工作,我发现另一个开发人员生成了一个空的 coffeescript 文件:
app/assets/javascript/application.coffee
我删除了这个文件,应用程序正常运行。库存 javascript_include_tag
现在读取清单并包含各种 javascript 模块,包括 jQuery、jQuery-ujs 和 turbolinks。