我无法理解此错误消息

时间:2016-01-03 18:52:03

标签: ruby-on-rails ruby

尝试启动我的rails服务器时出现错误:

home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/uri/rfc2396_parser.rb:305:in `escape': undefined method `gsub' for nil:NilClass (NoMethodError)

完整的控制台日志

=> Booting Puma
=> Rails 4.2.5 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
/home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/uri/rfc2396_parser.rb:305:in `escape': undefined method `gsub' for nil:NilClass (NoMethodError)
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/uri/common.rb:100:in `escape'
    from /home/vladislav/Desktop/metartaf-develop/config/initializers/redis.rb:1:in `<top (required)>'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `block in load'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:268:in `load'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/engine.rb:652:in `block in load_config_initializer'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/activesupport-4.2.5/lib/active_support/notifications.rb:166:in `instrument'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/engine.rb:651:in `load_config_initializer'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/engine.rb:616:in `block (2 levels) in <class:Engine>'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/engine.rb:615:in `each'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/engine.rb:615:in `block in <class:Engine>'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:228:in `block in tsort_each'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/initializable.rb:44:in `each'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/initializable.rb:44:in `tsort_each_child'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:415:in `call'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:415:in `each_strongly_connected_component_from'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:347:in `each'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:347:in `call'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:347:in `each_strongly_connected_component'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:226:in `tsort_each'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/2.4.0/tsort.rb:205:in `tsort_each'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/railtie.rb:194:in `public_send'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/railtie.rb:194:in `method_missing'
    from /home/vladislav/Desktop/metartaf-develop/config/environment.rb:5:in `<top (required)>'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `block in require'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/activesupport-4.2.5/lib/active_support/dependencies.rb:274:in `require'
    from /home/vladislav/Desktop/metartaf-develop/config.ru:3:in `block in <main>'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
    from /home/vladislav/Desktop/metartaf-develop/config.ru:in `new'
    from /home/vladislav/Desktop/metartaf-develop/config.ru:in `<main>'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/commands/server.rb:61:in `app'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/commands/server.rb:139:in `log_to_stdout'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/commands/server.rb:78:in `start'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/vladislav/.rbenv/versions/2.3.0-dev/lib/ruby/gems/2.4.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

这不是我的项目,所以我不知道错误在哪里。我在哪里有错?

#config/environment.rb 
        # Load the rails application.
        require File.expand_path('../application', __FILE__)

        # Initialize the rails application.
        Metartaf::Application.initialize!

#config/initializers/redis.rb
REDISTOGO_URL="redis://user@localhost:6379/"
uri = URI.parse(URI.encode(ENV["REDISTOGO_URL"]))
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

1 个答案:

答案 0 :(得分:3)

您正在使用ENV["REDISTOGO_URL"],因为该网址将在生产中提供(我猜您正在使用Heroku)。这很好,但是你使用ruby常量REDISTOGO_URL来在开发中使用本地URL,这不是一回事。

您收到的错误是因为您忽略了REDISTOGO_URL常量,仍然从ENV["REDISTOGO_URL"]读取值,我猜是nil

尝试替换:

REDISTOGO_URL="redis://user@localhost:6379/"
uri = URI.parse(URI.encode(ENV["REDISTOGO_URL"]))

使用:

REDISTOGO_URL = ENV["REDISTOGO_URL"] || "redis://user@localhost:6379/"
uri = URI.parse(URI.encode(REDISTOGO_URL))