【问题标题】:Rails Cannot require 'nokogiri/nokogiri', though IRB canRails 不能要求 'nokogiri/nokogiri',尽管 IRB 可以
【发布时间】:2018-01-30 07:28:03
【问题描述】:

首先,尽管我们的标题几乎相同,但this post 并没有解决这个问题。

两天前,我在开发模式下运行 rails,一切都很好。今天我启动服务器,我遇到了以下错误:

/Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/nokogiri-1.8.1/lib/nokogiri.rb:33:in `require': cannot load such file -- nokogiri/nokogiri (LoadError)
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/nokogiri-1.8.1/lib/nokogiri.rb:33:in `rescue in <top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/nokogiri-1.8.1/lib/nokogiri.rb:29:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/loofah-2.1.1/lib/loofah.rb:3:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/loofah-2.1.1/lib/loofah.rb:3:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/rails-html-sanitizer-1.0.3/lib/rails-html-sanitizer.rb:2:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/rails-html-sanitizer-1.0.3/lib/rails-html-sanitizer.rb:2:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/actionview-5.1.4/lib/action_view/helpers/sanitize_helper.rb:2:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/actionview-5.1.4/lib/action_view/helpers/sanitize_helper.rb:2:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/actionview-5.1.4/lib/action_view/helpers.rb:58:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/actionview-5.1.4/lib/action_view/helpers.rb:58:in `<module:Helpers>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/actionview-5.1.4/lib/action_view/helpers.rb:4:in `<module:ActionView>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/actionview-5.1.4/lib/action_view/helpers.rb:3:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/context.rb:1:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/context.rb:1:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sprockets-rails-3.2.1/lib/sprockets/railtie.rb:7:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sprockets-rails-3.2.1/lib/sprockets/railtie.rb:7:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sass-rails-5.0.6/lib/sass/rails/railtie.rb:3:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sass-rails-5.0.6/lib/sass/rails/railtie.rb:3:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sass-rails-5.0.6/lib/sass/rails.rb:11:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sass-rails-5.0.6/lib/sass/rails.rb:11:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sass-rails-5.0.6/lib/sass-rails.rb:1:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/sass-rails-5.0.6/lib/sass-rails.rb:1:in `<top (required)>'
from /Users/martin/.rvm/gems/ruby-2.4.1/gems/bundler-1.16.1/lib/bundler/runtime.rb:81:in `require'
from /Users/martin/.rvm/gems/ruby-2.4.1/gems/bundler-1.16.1/lib/bundler/runtime.rb:81:in `block (2 levels) in require'
from /Users/martin/.rvm/gems/ruby-2.4.1/gems/bundler-1.16.1/lib/bundler/runtime.rb:76:in `each'
from /Users/martin/.rvm/gems/ruby-2.4.1/gems/bundler-1.16.1/lib/bundler/runtime.rb:76:in `block in require'
from /Users/martin/.rvm/gems/ruby-2.4.1/gems/bundler-1.16.1/lib/bundler/runtime.rb:65:in `each'
from /Users/martin/.rvm/gems/ruby-2.4.1/gems/bundler-1.16.1/lib/bundler/runtime.rb:65:in `require'
from /Users/martin/.rvm/gems/ruby-2.4.1/gems/bundler-1.16.1/lib/bundler.rb:114:in `require'
from /Users/martin/Desktop/hotline/config/application.rb:7:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `require'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:133:in `block in perform'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
from /Users/martin/Desktop/hotline/bin/rails:9:in `require'
from /Users/martin/Desktop/hotline/bin/rails:9:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `load'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in `call'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/spring-2.0.2/lib/spring/client/command.rb:7:in `call'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/spring-2.0.2/lib/spring/client.rb:30:in `run'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/spring-2.0.2/bin/spring:49:in `<top (required)>'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `load'
from /Users/martin/Desktop/hotline/vendor/bundle/ruby/2.4.0/gems/spring-2.0.2/lib/spring/binstub.rb:31:in `<top (required)>'
from /Users/martin/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:70:in `require'
from /Users/martin/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:70:in `require'
from /Users/martin/Desktop/hotline/bin/spring:15:in `<top (required)>'
from bin/rails:3:in `load'
from bin/rails:3:in `<main>'

我总结的意思是,当 rails 调用 `require 'nokogiri/nokogiri' 时,rails 找不到 gem 并且它失败了。我的第一个想法是检查我是否有宝石,所以我打电话给

>>irb
2.4.1 :001 > require 'nokogiri/nokogiri'
 => true 

我的下一个想法是重新加载 nokogiri,所以我卸载并重新安装了几次,然后重新启动了我的 Mac,但无济于事。

我在网上搜索了一个咒语,我发现最接近我的解决方案的人是 Windows 存在兼容性问题。这非常无用,因为我有一台 Mac。

现在,我什至不知道我不知道什么。为什么会触发此错误?从现在到两天前,我的电脑发生了什么变化导致这个错误?我需要做什么来解决这个问题?

【问题讨论】:

  • 在您的irb 中,RUBY_VERSION 常量返回什么?
  • RUBY_VERSION 返回=&gt; "2.4.1"
  • 当您安装 gem 时——通过gem install 或使用bundle——它特定于那个 Ruby 环境。您的 Rails 应用程序是为 2.4.0 捆绑的,而 Nokogiri 是为 2.4.1 安装的。将您的 Ruby 环境设置为正确的版本并再次运行 bundle install,重新启动您的 Rails 服务器,您应该已经设置好了。

标签: ruby-on-rails rubygems nokogiri


【解决方案1】:

叹息 我发现了问题。如果您像我一样将最近的项目保存在桌面上,Mac OS 将尝试通过将文件上传到云来节省磁盘空间。如果您的一半应用程序在云中,当您尝试运行本地主机时,您的一半应用程序将不存在。我从云端重新下载了文件,它运行得很好。

【讨论】:

    猜你喜欢
    • 2011-08-30
    • 2012-12-06
    • 2016-04-07
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    相关资源
    最近更新 更多