enter image description here

在从Rails 5.0升级到5.1之后,每当应用程序重新加载时,我都会遇到这个错误,可能是因为rails server年期间的代码更改,或者如果我从控制台调用reload!.

? 13:53$ rc
Loading development environment (Rails 5.1.1)
2.3.1 :001 > reload!
Reloading...
ArgumentError: unknown firstpos: NilClass
    from (irb):1
2.3.1 :002 > 

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/journey/gtg/builder.rb

应用程序跟踪为空,以下是框架跟踪:

actionpack (5.1.1) lib/action_dispatch/journey/gtg/builder.rb:99:in `firstpos'
actionpack (5.1.1) lib/action_dispatch/journey/gtg/builder.rb:22:in `transition_table'
actionpack (5.1.1) lib/action_dispatch/journey/routes.rb:58:in `simulator'
actionpack (5.1.1) lib/action_dispatch/journey/router.rb:92:in `simulator'
actionpack (5.1.1) lib/action_dispatch/journey/router.rb:28:in `eager_load!'
actionpack (5.1.1) lib/action_dispatch/routing/route_set.rb:382:in `eager_load!'
railties (5.1.1) lib/rails/application/routes_reloader.rb:26:in `each'
railties (5.1.1) lib/rails/application/routes_reloader.rb:26:in `execute'
railties (5.1.1) lib/rails/application/finisher.rb:141:in `block (2 levels) in <module:Finisher>'
activesupport (5.1.1) lib/active_support/callbacks.rb:413:in `instance_exec'
activesupport (5.1.1) lib/active_support/callbacks.rb:413:in `block in make_lambda'
activesupport (5.1.1) lib/active_support/callbacks.rb:197:in `block (2 levels) in halting'
activesupport (5.1.1) lib/active_support/callbacks.rb:601:in `block (2 levels) in default_terminator'
activesupport (5.1.1) lib/active_support/callbacks.rb:600:in `catch'
activesupport (5.1.1) lib/active_support/callbacks.rb:600:in `block in default_terminator'
activesupport (5.1.1) lib/active_support/callbacks.rb:198:in `block in halting'
activesupport (5.1.1) lib/active_support/callbacks.rb:507:in `block in invoke_before'
activesupport (5.1.1) lib/active_support/callbacks.rb:507:in `each'
activesupport (5.1.1) lib/active_support/callbacks.rb:507:in `invoke_before'
activesupport (5.1.1) lib/active_support/callbacks.rb:130:in `run_callbacks'
activesupport (5.1.1) lib/active_support/execution_wrapper.rb:108:in `run!'
activesupport (5.1.1) lib/active_support/reloader.rb:113:in `run!'
activesupport (5.1.1) lib/active_support/execution_wrapper.rb:70:in `block in run!'
activesupport (5.1.1) lib/active_support/execution_wrapper.rb:67:in `tap'
activesupport (5.1.1) lib/active_support/execution_wrapper.rb:67:in `run!'
activesupport (5.1.1) lib/active_support/reloader.rb:59:in `run!'
actionpack (5.1.1) lib/action_dispatch/middleware/executor.rb:10:in `call'
actionpack (5.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
actionpack (5.1.1) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.1) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.1) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.1) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.1) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.1) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.1) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
request_store (1.3.2) lib/request_store/middleware.rb:9:in `call'
actionpack (5.1.1) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.1) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.1) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.1.1) lib/rails/engine.rb:522:in `call'
puma (3.9.1) lib/puma/configuration.rb:224:in `call'
puma (3.9.1) lib/puma/server.rb:602:in `handle_request'
puma (3.9.1) lib/puma/server.rb:435:in `process_client'
puma (3.9.1) lib/puma/server.rb:299:in `block in run'
puma (3.9.1) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

推荐答案

找到解决方法!https://github.com/rails/rails/pull/32296

pull请求没有合并,可能只会出现在5.2+版本中.添加一个带有一行更改的猴子补丁完全解决了我的问题.

配置/初始化器/路由.rb

# MONKEY PATCH!!!
# https://github.com/rails/rails/pull/32296
#
# Fixes:
# * Development mode deadlocks
# * ArgumentError: unknown firstpos: NilClass
#
# Allows use of "config.eager_load = true"


module ActionDispatch
  module Journey
    class Routes
      def simulator
        @simulator ||= begin
          gtg = GTG::Builder.new(ast).transition_table unless ast.blank?
          GTG::Simulator.new(gtg)
        end
      end
    end
  end
end

Ruby-on-rails相关问答推荐

Rails 7 Active Record::SessionStore -如何获取控制器中当前会话的数据库ID?

如何测试自定义路由?

form_with正在try 用错误的路径生成操作

Rails + Turbo_stream自定义操作:我可以在没有Stimulus的情况下根据DOM状态做出有条件的响应吗?

Rails 7 共享刺激控制器功能

为 ActionMailer 渲染不同的视图(模板)

bundle和gem安装的区别?

在 Windows 上安装特定的 Ruby on Rails 版本

删除链接在 Rails 3 视图中发送Get而不是Delete

Rails 3.1 插件 gem、虚拟测试应用程序、rspec

参数错误:范围主体需要可调用

我如何在具有活动记录的 Rails 中使用两个不同的数据库?

Rails Associations - has_many => :through - 但相同的模型

ActiveRecord 事务中的错误处理?

Ruby 根据属性在数组中查找并返回对象

Test::Unit Rails - 如何断言一个数字大于另一个?

扩展 Devise SessionsController 以使用 JSON 进行身份验证

如何在 Rails 3 中订购包含的元素

将数组传递到 hidden_​​field ROR

设置EDITOR或BUNDLER_EDITOR环境变量