Rails 4 jQuery,javascript和coffee脚本不起作用

时间:2015-05-21 20:07:49

标签: javascript jquery ruby-on-rails ruby coffeescript

我是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

<%= 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>
...

的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 <%= 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

配置/ 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之外它们都运行良好。我没有得到任何预期的行为,所以在我的“人物”索引视图中我添加了:

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

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

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

我也尝试过包含jquery-turbolinks gem,尽管我很确定jquery-rails gem不需要它。但它也不适用于卸载的turbolinks,所以我认为这不是冲突。

感谢您的光临。

4 个答案:

答案 0 :(得分:3)

要解决此问题,我创建了一个空白的应用程序,只有一个'welcome'控制器,只包含jQuery测试脚本,它运行正常。

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

app/assets/javascript/application.coffee

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

答案 1 :(得分:0)

Rails 4自动将sass-rails,coffee-rails和uglifier gems添加到您的Gemfile中,Sprockets用它来进行资产压缩。所以没有必要明确添加gem。

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

答案 2 :(得分:0)

尝试添加到config / application.rb

config.assets.enabled = true

您可能还需要:

config.assets.initialize_on_precompile = true

答案 3 :(得分:0)

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

 <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适用于您的应用程序

相关问题