简体   繁体   English

运行rails控制台时加载错误

[英]Load error when running rails console

I am using rails 4.1 and ruby 2.1.1 我使用rails 4.1和ruby 2.1.1

Everything works, but when I run rails console I get this error 一切正常,但是当我运行rails console时,我得到了这个错误

> rails console
Loading development environment (Rails 4.1.0)
load error: /home/andreas/.rvm/rubies/ruby-2.1.1/.irbrc
NoMethodError: undefined method `split' for nil:NilClass
    /home/andreas/.rvm/scripts/irbrc.rb:41:in `<top (required)>'

After the error the console opens and can be used. 错误后控制台打开并可以使用。

Here's the 41th line and surroundings in the .irbrc file. 这是.irbrc文件中的第41行和周围环境。

39 # Calculate the ruby string.
40 rvm_ruby_string = ENV["rvm_ruby_string"] ||
41 (ENV['GEM_HOME'] && ENV['GEM_HOME'].split(/\//).last.split(/@/).first) ||
42 ("#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" rescue nil) ||
43 (RUBY_DESCRIPTION.split(" ")[1].sub('p', '-p') rescue nil ) ||
44 (`ruby -v` || '').split(" ")[1].sub('p', '-p')

I get these results when testing in the console 在控制台中测试时,我得到了这些结果

irb(main):008:0> ENV['GEM_HOME']
=> ""
irb(main):009:0> ENV['GEM_HOME'].split(/\//).last
=> nil

If I run irb outside the rails application, I get 如果我在rails应用程序之外运行irb,我会得到

2.1.1 :001 > ENV['GEM_HOME']
 => "/home/andreas/.rvm/gems/ruby-2.1.1" 
2.1.1 :002 > ENV['GEM_HOME'].split(/\//).last
 => "ruby-2.1.1" 

Do you know why the environment variable is blank in the rails application? 你知道为什么rails应用程序中的环境变量是空白的吗?

If you encounter this problem you should restart you computer . 如果遇到此问题,则应重新启动计算机 If that does not fix it read on. 如果这不能解决,请继续阅读。

The bin/spring file sets ENV["GEM_HOME"] to a blank string bin / spring文件将ENV [“GEM_HOME”]设置为空字符串

bin/spring 斌/春天

11 ENV["GEM_PATH"] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR)
12 ENV["GEM_HOME"] = ""
13 Gem.paths = ENV

This crashes when running rails console because in line 41 运行rails控制台时崩溃,因为在第41行

ENV['GEM_HOME'].split(/\//).last

returns nil if ENV['GEM_HOME'] is blank 如果ENV['GEM_HOME']为空,则返回nil

~/.rvm/rubies/ruby-2.1.1/.irbrc 〜/ .rvm /红宝石/红宝石2.1.1 / .irbrc

39 # Calculate the ruby string.
40 rvm_ruby_string = ENV["rvm_ruby_string"] ||
41 (ENV['GEM_HOME'] && ENV['GEM_HOME'].split(/\//).last.split(/@/).first) ||
42 ("#{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}" rescue nil) ||
43 (RUBY_DESCRIPTION.split(" ")[1].sub('p', '-p') rescue nil ) ||
44 (`ruby -v` || '').split(" ")[1].sub('p', '-p')

rvm uses the string to set the prompt message in the console. rvm使用该字符串在控制台中设置提示消息。 If you change line 12 in bin/spring to 如果您将bin / spring中的第12行更改为

ENV["GEM_HOME"] = "Spring is great!"

You get this nice prompt 你得到了这个很好的提示

bin/rails c
Loading development environment (Rails 4.1.0)
Spring is great! :001 > 

I don't really understand why ENV["GEM_HOME"] is set to a blank string. 我真的不明白为什么ENV["GEM_HOME"]被设置为空字符串。 So, I just change this to get rid of the error. 所以,我只是改变它来摆脱错误。 I have posted an issue on the spring github page. 我在spring github页面上发布了一个问题。

Beware! 谨防!

Any changes to the bin/spring file gets overwritten when you run the spring binstub command 运行spring binstub命令时,bin / spring文件的任何更改都会被覆盖

I just encountered the same problem, and I solved it by spring stop . 我刚遇到同样的问题,我在spring stop解决了。 I think there is no need to restart the computer. 我认为没有必要重新启动计算机。 What you should do is to restart spring. 你应该做的是重新开始弹簧。

These steps solved my problem: 这些步骤解决了我的问题:

  • find the current ruby version 找到当前的红宝石版本

ruby -v 红宝石-v

example: 例:

ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0] ruby 2.1.2p95(2014-05-08修订版45877)[x86_64-darwin13.0]

  • remove this version 删除此版本

rvm remove 2.1.2 rvm删除2.1.2

  • install again 再次安装

rvm install 2.1.2 rvm install 2.1.2

You may just need to specify a gemset with 您可能只需要指定一个gemset

rvm gemset use YourGemset

I find it's a good idea to use a separate gemset for each project. 我发现为每个项目使用单独的gemset是个好主意。 Then place the name of that project's gemset in a file named .ruby-gemset in the root of your project. 然后将该项目的gemset的名称放在项目根目录中名为.ruby-gemset的文件中。 See https://rvm.io/workflow/projects for more info. 有关详细信息,请参阅https://rvm.io/workflow/projects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM