找不到类型为“application/javascript”的文件“jquery”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31102282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:46:51  来源:igfitidea点击:

couldn't find file 'jquery' with type 'application/javascript'

jqueryruby-on-railsruby-on-rails-4assets

提问by Souvik Das

This might seem a repeated question but none of the solutions seem to work for me. I have gem jquery-railsin my Gemfile. Also, the lines

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

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

And this is my Gemfile:

这是我的 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]

My application.html.erb :

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

I cannot start my Rails application. I do not understand what is the issue. My ruby version is 2.1.6 Please help.

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

回答by novice_dev

I am following RoR 4. I came across the same error couldn't find file 'jquery' with type 'application/javascript'. I did the following and was successful in running my app

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

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

回答by s Narayanaswamy

This occurs often times because the Gemfile lacks the gems needed for the jquery dependencies to resolve themselves.

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

In Rails 5.x, add this to the Gemfile:

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

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

After this, run bundle Install.

在此之后,运行 bundle Install。

Restart the server for it to update the latest changes and voila, it works!

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

回答by Souvik Das

This is caused because of default javascript runtimes that are already available in Windows but has some compatibility issues. The most easy workaround is to install nodejs from http://nodejs.org/download/and then restart your PC. This solves all the issues.

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

For more details about the issue, please refer to this page : ExecJS::RuntimeError on Windows trying to follow rubytutorial

有关该问题的更多详细信息,请参阅此页面:尝试遵循 ruby​​tutorial 的 Windows 上的 ExecJS::RuntimeError

回答by Gabe

In your controller file you can not tell rails not to use a layout by writing

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

layout false

Directly under your controller class on the second line

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

class ExampleController < ApplicationController
layout false

回答by Wilson

Go to gemfile and make sure that gem jquery-railsis uncommented. Run bundle install. That should do it.

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

回答by Adittya Verma

You simply need to add gem 'jquery-rails' in Gemfile and run bundle install. It worked for me.

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

回答by dreamBlitzer

This issue can be fixed by the following steps bellow.

可以通过以下步骤解决此问题。

  1. Make sure that the require jquery statement is there in application.js

    //= require jquery

    //= require jquery_ujs

  2. Make sure that all the required gems are present in the Gemfile, if it is not present add it and execute bundle installcommand.

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

    gem 'rails-ujs', '~> 0.1.0'3.'

  3. If it is still showing the error update your node by runnig

    sudo npm update npm -g

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

    //= require jquery

    //= require jquery_ujs

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

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

    gem 'rails-ujs', '~> 0.1.0'3.'

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

    sudo npm update npm -g

Finally refresh your application.

最后刷新您的应用程序。

回答by H. Almidan

You could also try running gem install jquery-railsin your terminal. It worked for me.

您也可以尝试gem install jquery-rails在终端中运行。它对我有用。