【问题标题】:couldn't find file 'jquery' with type 'application/javascript'找不到类型为“application/javascript”的文件“jquery”
【发布时间】:2015-09-15 03:00:46
【问题描述】:

这似乎是一个重复的问题,但似乎没有一个解决方案适合我。我的 Gemfile 中有 gem jquery-rails。另外,线条

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

这是我的 Gemfile:

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.1'

# 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/rails/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

# 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'
  # Use mysql as the database for Active Record
  gem 'mysql2'
  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

我的 application.html.erb :

<!DOCTYPE html>
<html>
<head>
  <title></title>
  <%= stylesheet_link_tag 'application', media: 'all' %>
  <%= javascript_include_tag 'application' %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

我无法启动我的 Rails 应用程序。我不明白是什么问题。我的红宝石版本是 2.1.6 请帮忙。

【问题讨论】:

  • 您是否运行了捆绑安装?
  • 是的,我已尝试捆绑安装并重新启动服务器。我不明白出了什么问题。
  • Rails 应该从 gem 加载 javascript 文件。尝试运行 bundle exec rails s 以确保您使用的是正确的 gem。
  • 我明白了。这是一个平台问题。需要将&lt;%= javascript_include_tag 'application' %&gt; 更改为&lt;%= javascript_include_tag 'default' %&gt;
  • 目前使用 Fedora 20。从 Gemfile 卸载 nodejs 并取消注释 ruby​​racer gem 就可以解决问题。仅当错误消息更清楚一点时。

标签: jquery ruby-on-rails ruby-on-rails-4 assets


【解决方案1】:

我关注RoR 4。我遇到了同样的错误couldn't find file 'jquery' with type 'application/javascript'。我做了以下操作并成功运行了我的应用程序

  • gem 'jquery-rails'gem 'turbolinks'
  • bundle update
  • bundle install

【讨论】:

  • 但是如果你在一个最近没有更新过的项目中,请小心bundle update。这将更新您的 Gemfile 的每个 gem,并且使其他东西停止工作是很常见的。
  • @bonafernando 谢谢 :) 我一定会记住这一点。
  • 阻止 gem 更新在 Gemfile 中提及它的版本号。
【解决方案2】:

这种情况经常发生,因为 Gemfile 缺少 jquery 依赖项自行解析所需的 gem。

在 Rails 5.x 中,将其添加到 Gemfile:

gem 'jquery-rails', '~> 4.3', '>= 4.3.3'
gem 'rails-ujs', '~> 0.1.0'

在此之后,运行捆绑安装。

重新启动服务器以更新最新更改,瞧,它可以工作了!

【讨论】:

  • gem 'jquery-rails' 已经足够了
【解决方案3】:

这是因为默认的 javascript 运行时已在 Windows 中可用,但存在一些兼容性问题。最简单的解决方法是从http://nodejs.org/download/ 安装nodejs,然后重新启动您的PC。这解决了所有问题。

有关此问题的更多详细信息,请参阅此页面:ExecJS::RuntimeError on Windows trying to follow rubytutorial

【讨论】:

    【解决方案4】:

    在你的控制器文件中,你不能通过写来告诉 Rails 不要使用布局

    layout false
    

    直接在你的控制器类下的第二行

    class ExampleController < ApplicationController
    layout false
    

    【讨论】:

      【解决方案5】:

      这个问题可以通过下面的步骤来解决。

      1. 确保 application.js 中有 require jquery 语句

        //= require jquery

        //= require jquery_ujs

      2. 确保所有必需的 gem 都存在于 Gemfile 中,如果不存在的话 现在添加它并执行bundle install命令。

        gem 'jquery-rails', '~&gt; 4.3', '&gt;= 4.3.3'

        gem 'rails-ujs', '~&gt; 0.1.0'3.'

      3. 如果仍然显示错误,请通过运行更新您的节点

        sudo npm update npm -g

      最后刷新你的应用程序。

      【讨论】:

        【解决方案6】:

        转到 gemfile 并确保 gem jquery-rails 未注释。运行捆绑安装。应该这样做。

        【讨论】:

          【解决方案7】:

          您只需在 Gemfile 中添加 gem 'jquery-rails' 并运行 bundle install。它对我有用。

          【讨论】:

            猜你喜欢
            • 2020-09-02
            • 2018-01-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-03-18
            • 2019-09-20
            相关资源
            最近更新 更多