Rails 4 jQuery,JavaScript和Coffee脚本不起作用 [英] Rails 4 jQuery, javascript and coffee scripts not working

查看:76
本文介绍了Rails 4 jQuery,JavaScript和Coffee脚本不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails和Web开发的新手,尽管我在控制系统和固件方面拥有近二十年的C/C ++知识,并且拥有大量的Shell和Perl脚本.

I am new to rails and web development, although I have almost two decades of C/C++ in control systems and firmware, and quite a lot of shell and perl scripting.

即使jquery在application.js清单中,我也无法在没有显式包含它的情况下使其工作,而且我也无法使任何单独的咖啡脚本全部工作.

I can't get jquery to work without explicitly including it, even though it is in the application.js manifest, and I can't get any of the individual coffeescripts to work at all.

Ubuntu 14.04LTS,红宝石2.2.1p85,导轨4.2.0

Ubuntu 14.04LTS, ruby 2.2.1p85, rails 4.2.0

application.js

application.js

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

application.html.erb

application.html.erb

<%= render "layouts/header" %>
<%= render "layouts/sidenav" %>
<%= yield %>
<%= render "layouts/footer" %>

_header.html.erb

_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

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.我没有得到任何预期的行为,因此在人"索引视图中添加了:

I have eleven controllers and views out of 27 built, and they all work well except that I hadn't tried any javascript until yesterday. I wasn't getting any of the expected behavior, so in my "people" index view I added:

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

检查jQuery是否在正常工作.如果在csrf_meta_tag之前添加<%= javascript_include_tag 'jquery.js' %>,我会收到警报消息,但是coffeescript和其他消息仍然无效.没有显式的jQuery include,我什么也得不到.

To check and see if jQuery was working at all. If I add <%= javascript_include_tag 'jquery.js' %> before the csrf_meta_tag, I get the alert message but coffeescript and the rest still don't work. Without the explicit jquery include, I get nothing.

我看过很多关于此的文章,但都没有奏效.在我看来,未读取application.js中的清单和/或不包含列出的模块,只是当在页面之间移动时,turbolink在浏览器的网络面板上似乎可以正常工作.我删除了turbolink,但其行为却大不相同.

I've looked at many, many posts on this, but none of them have worked. It appears to me that the manifest in application.js is not being read and/or the modules listed are not included, except that turbolink appears to work properly gauging by the network panel on the browser when moving from page to page. I removed turbolink and it behaves very differently.

我也尝试过包括jquery-turbolinks gem,尽管我很确定jquery-rails gem不需要它.但这也不适用于已卸载的turbolinks,因此我不认为这是冲突.

I also tried including the jquery-turbolinks gem, although even though I'm pretty sure it's not needed with the jquery-rails gem. But it doesn't work with turbolinks uninstalled, either, so I don't think that is the conflict.

感谢您的光临.

推荐答案

为解决此问题,我创建了一个空白应用程序,其中只有一个"welcome"控制器,仅包含jQuery测试脚本,效果很好.

To resolve this issue I created a blank app with just a 'welcome' controller, containing only the jQuery test script, which worked fine.

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

Working backwards from the differences in the trees from the naked application, I found that another developer had generated an empty coffeescript file:

app/assets/javascript/application.coffee

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

I removed this file and the application works properly. The stock javascript_include_tag now reads the manifest and includes the various javascript modules, including jQuery, jQuery-ujs and turbolinks.

这篇关于Rails 4 jQuery,JavaScript和Coffee脚本不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆