运行 rails 控制台时加载错误 [英] Load error when running rails console

查看:34
本文介绍了运行 rails 控制台时加载错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 rails 4.1 和 ruby​​ 2.1.1

I am using rails 4.1 and ruby 2.1.1

一切正常,但是当我运行 rails 控制台时出现此错误

Everything works, but when I run rails console I get this error

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

这是 .irbrc 文件中的第 41 行和周围环境.

Here's the 41th line and surroundings in the .irbrc file.

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

如果我在 rails 应用程序之外运行 irb,我会得到

If I run irb outside the rails application, I get

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" 

你知道为什么rails应用中环境变量为空吗?

Do you know why the environment variable is blank in the rails application?

推荐答案

如果您遇到此问题您应该重新启动计算机.如果这不能解决它,请继续阅读.

If you encounter this problem you should restart you computer. If that does not fix it read on.

bin/spring 文件将 ENV["GEM_HOME"] 设置为空字符串

The bin/spring file sets ENV["GEM_HOME"] to a blank string

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

运行 rails 控制台时会崩溃,因为在第 41 行

This crashes when running rails console because in line 41

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

如果 ENV['GEM_HOME'] 为空则返回 nil

returns nil if ENV['GEM_HOME'] is blank

~/.rvm/rubies/ruby-2.1.1/.irbrc

~/.rvm/rubies/ruby-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 使用字符串设置控制台中的提示信息.如果将 bin/spring 中的第 12 行更改为

rvm uses the string to set the prompt message in the console. If you change line 12 in bin/spring to

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 > 

我真的不明白为什么 ENV["GEM_HOME"] 被设置为一个空字符串.所以,我只是改变它以摆脱错误.我已经在 spring github 页面上发布了一个问题.

I don't really understand why ENV["GEM_HOME"] is set to a blank string. So, I just change this to get rid of the error. I have posted an issue on the spring github page.

当您运行 spring binstub 命令时,对 bin/spring 文件的任何更改都会被覆盖

Any changes to the bin/spring file gets overwritten when you run the spring binstub command

这篇关于运行 rails 控制台时加载错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆