Ruby on Rails仅在第一次加载时出现NoMethodError

时间:2011-09-16 23:26:02

标签: ruby-on-rails ruby nginx passenger

我的应用程序第一次加载时,出现错误。如果我立即重新加载页面,应用程序加载正常。这在开发中不是什么大问题,但在生产中它似乎缓存了错误。我已经尝试过ruby 1.9.2-p136,1.9.2-p180,1.9.2-p290,但它们似乎都是这样做的。以下是开发环境的输出。进一步向下是来自nginx的日志,它产生502 Bad Gateway,似乎缓存了NoMethodError。

$ rails s
=> Booting WEBrick
=> Rails 3.0.5 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2011-09-16 19:13:39] INFO  WEBrick 1.3.1
[2011-09-16 19:13:39] INFO  ruby 1.9.2 (2011-07-09) [i686-linux]
[2011-09-16 19:13:39] INFO  WEBrick::HTTPServer#start: pid=2260 port=3000

Started GET "/" for 127.0.0.1 at 2011-09-16 19:13:42 -0400

NoMethodError (undefined method `[]' for false:FalseClass):

Rendered /home/paisley/tool/install/ruby-1.9.2-p290-fts/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_trace.erb (0.9ms)
Rendered /home/paisley/tool/install/ruby-1.9.2-p290-fts/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (6.2ms)
Rendered /home/paisley/tool/install/ruby-1.9.2-p290-fts/lib/ruby/gems/1.9.1/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.1ms)

Started GET "/" for 127.0.0.1 at 2011-09-16 19:13:44 -0400
  Processing by WelcomeController#index as HTML
Rendered welcome/index.haml within layouts/application (13.6ms)
Completed 200 OK in 18ms (Views: 17.5ms)

下面的nginx错误日志:

2011/09/16 19:21:29 [error] 15002#0: *30 upstream prematurely closed connection while reading response header from upstream, client: 74.71.201.247, server: redacted.com, request: "GET / HTTP/1.1", upstream: "passenger:unix:/passenger_helper_server:", host: "redacted.com"
[ pid=19637 thr=74145680 file=utils.rb:176 time=2011-09-16 19:21:29.767 ]: *** Exception NoMethodError in application (undefined method `[]' for false:FalseClass) (process 19637, thread #<Thread:0x8d6bf20>):
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:59:in `establish_connection'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:55:in `establish_connection'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/railtie.rb:59:in `block (2 levels) in <class:Railtie>'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/activerecord-3.0.5/lib/active_record/base.rb:1900:in `<top (required)>'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/utils.rb:397:in `before_handling_requests'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/rack/application_spawner.rb:204:in `start_request_handler'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/rack/application_spawner.rb:170:in `block in handle_spawn_application'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/utils.rb:479:in `safe_fork'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/rack/application_spawner.rb:165:in `handle_spawn_application'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/abstract_server.rb:180:in `start'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/rack/application_spawner.rb:128:in `start'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/spawn_manager.rb:253:in `block (2 levels) in spawn_rack_application'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/abstract_server_collection.rb:132:in `lookup_or_add'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/spawn_manager.rb:246:in `block in spawn_rack_application'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/abstract_server_collection.rb:82:in `block in synchronize'
    from <internal:prelude>:10:in `synchronize'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/abstract_server_collection.rb:79:in `synchronize'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/spawn_manager.rb:244:in `spawn_rack_application'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/spawn_manager.rb:137:in `spawn_application'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/spawn_manager.rb:275:in `handle_spawn_application'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/abstract_server.rb:357:in `server_main_loop'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/lib/phusion_passenger/abstract_server.rb:206:in `start_synchronously'
    from /opt/tool/install/ruby-1.9.2-p180-tasks/lib/ruby/gems/1.9.1/gems/passenger-3.0.2/helper-scripts/passenger-spawn-server:99:in `<main>'

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您的应用程序不使用ActiveRecord,您应该以不同的方式设置项目。

在你的Gemfile中,不要添加'rails',只需添加:

RAILS_VERSION = '~> 3.0'

gem 'activesupport',      RAILS_VERSION, :require => 'active_support'
gem 'actionpack',         RAILS_VERSION, :require => 'action_pack'
gem 'actionmailer',       RAILS_VERSION, :require => 'action_mailer'
gem 'railties',           RAILS_VERSION, :require => 'rails'

除了ActiveRecord,这就是Rails中的所有内容。

现在你还需要修改你的config / application.rb,而不是require "rails",它只是:

require "action_controller/railtie"
require "action_mailer/railtie"
require "rails/test_unit/railtie"

如果你没有使用Test :: Unit,请从上面的摘录中删除该铁轨。