gpt4 book ai didi

javascript - Rails 4 jQuery、javascript 和 CoffeeScript 无法正常工作

转载 作者:行者123 更新时间:2023-11-28 19:13:19 25 4
gpt4 key购买 nike

尽管我在控制系统和固件方面拥有近二十年的 C/C++ 经验,并且掌握了大量 shell 和 perl 脚本,但我对 Rails 和 Web 开发还是个新手。

如果不显式包含 jquery,我就无法让 jquery 工作,即使它位于 application.js list 中,而且我根本无法让任何单独的 Coffeescript 工作。

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

gem 文件

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 个 Controller 和 View ,其中有 11 个 Controller 和 View ,它们都运行良好,只是直到昨天我才尝试过任何 javascript。我没有得到任何预期的行为,因此在我的“人员”索引 View 中添加了:

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

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

我看过很多很多关于此的帖子,但没有一个起作用。在我看来,application.js 中的 list 没有被读取和/或列出的模块不包括在内,除了在从一个页面移动到另一个页面时,turbolink 似乎可以通过浏览器上的网络面板正常工作。我删除了 Turbolink,它的行为非常不同。

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

感谢您的浏览。

最佳答案

为了解决这个问题,我创建了一个空白应用程序,只有一个“欢迎” Controller ,仅包含 jQuery 测试脚本,效果很好。

从裸应用程序树中的差异开始回溯,我发现另一个开发人员生成了一个空的 Coffeescript 文件:

app/assets/javascript/application.coffee

我删除了该文件,应用程序正常工作。库存 javascript_include_tag 现在读取 list 并包含各种 javascript 模块,包括 jQuery、jQuery-ujs 和 Turbolinks。

关于javascript - Rails 4 jQuery、javascript 和 CoffeeScript 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com