我们最近更新了应用的ruby和rubygems版本,并且我们一直遇到以下错误:
A TypeError occurred in [model]#[view]:
String can't be coerced into Integer
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_view/template/error.rb:92:in `+'
这是回溯:
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_view/template/error.rb:92:in `+'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_view/template/error.rb:92:in `sum'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_view/template/error.rb:92:in `source_extract'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_view/template/error.rb:112:in `annoted_source_code'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_dispatch/middleware/debug_exceptions.rb:68:in `block in log_error'
vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.22.5/lib/active_support/deprecation/reporting.rb:20:in `silence'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_dispatch/middleware/debug_exceptions.rb:66:in `log_error'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_dispatch/middleware/debug_exceptions.rb:34:in `render_exception'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_dispatch/middleware/debug_exceptions.rb:27:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/exception_notification-4.1.1/lib/exception_notification/rack.rb:32:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/railties-3.2.22.5/lib/rails/rack/logger.rb:32:in `call_app'
vendor/bundle/ruby/2.5.0/gems/railties-3.2.22.5/lib/rails/rack/logger.rb:16:in `block in call'
vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.22.5/lib/active_support/tagged_logging.rb:22:in `tagged'
vendor/bundle/ruby/2.5.0/gems/railties-3.2.22.5/lib/rails/rack/logger.rb:16:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_dispatch/middleware/request_id.rb:22:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/rack-1.4.7/lib/rack/methodoverride.rb:21:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/rack-1.4.7/lib/rack/runtime.rb:17:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.22.5/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/rack-1.4.7/lib/rack/lock.rb:15:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/rack-1.4.7/lib/rack/deflater.rb:13:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/actionpack-3.2.22.5/lib/action_dispatch/middleware/static.rb:83:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/rack-ssl-1.3.4/lib/rack/ssl.rb:27:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/rack-cache-1.7.2/lib/rack/cache/context.rb:140:in `forward'
vendor/bundle/ruby/2.5.0/gems/rack-cache-1.7.2/lib/rack/cache/context.rb:249:in `fetch'
vendor/bundle/ruby/2.5.0/gems/rack-cache-1.7.2/lib/rack/cache/context.rb:189:in `lookup'
vendor/bundle/ruby/2.5.0/gems/rack-cache-1.7.2/lib/rack/cache/context.rb:66:in `call!'
vendor/bundle/ruby/2.5.0/gems/rack-cache-1.7.2/lib/rack/cache/context.rb:51:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
lib/rack/assets.rb:167:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
lib/rack/basics.rb:24:in `call'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/railties-3.2.22.5/lib/rails/engine.rb:484:in `call'
vendor/bundle/ruby/2.5.0/gems/railties-3.2.22.5/lib/rails/application.rb:231:in `call'
vendor/bundle/ruby/2.5.0/gems/railties-3.2.22.5/lib/rails/railtie/configurable.rb:30:in `method_missing'
vendor/bundle/ruby/2.5.0/gems/newrelic_rpm-5.1.0.344/lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.0/lib/unicorn/http_server.rb:606:in `process_client'
config/unicorn.rb:94:in `process_client_with_gc'
vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.0/lib/unicorn/http_server.rb:701:in `worker_loop'
vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.0/lib/unicorn/http_server.rb:549:in `spawn_missing_workers'
vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.0/lib/unicorn/http_server.rb:142:in `start'
vendor/bundle/ruby/2.5.0/gems/unicorn-5.4.0/bin/unicorn:126:in `<top (required)>'
vendor/bundle/ruby/2.5.0/bin/unicorn:23:in `load'
vendor/bundle/ruby/2.5.0/bin/unicorn:23:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `load'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:75:in `kernel_load'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/cli/exec.rb:28:in `run'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/cli.rb:424:in `exec'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/cli.rb:27:in `dispatch'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/cli.rb:18:in `start'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/exe/bundle:30:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/lib/bundler/friendly_errors.rb:122:in `with_friendly_errors'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/gems/bundler-1.16.1/exe/bundle:22:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/bin/bundle:23:in `load'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/bin/bundle:23:in `<main>'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.5.1-tagX4gpqkt9tzqft/bin/ruby_executable_hooks:15:in `<main>'
我试图理解为什么会发生这种错误。基于回溯,它似乎不是由我们的代码引起的,因为它只发生在网站的某些页面上,我不认为它是一个宝石兼容性问题。值得注意的是,ActionView :: Template :: Error代码在字符串数组上调用sum:
79 def source_extract(indentation = 0)
80 return unless num = line_number
81 num = num.to_i
82
83 source_code = @template.source.split("\n")
84
85 start_on_line = [ num - SOURCE_CODE_RADIUS - 1, 0 ].max
86 end_on_line = [ num + SOURCE_CODE_RADIUS - 1, source_code.length].min
87
88 indent = ' ' * indentation
89 line_counter = start_on_line
90 return unless source_code = source_code[start_on_line..end_on_line]
91
92 source_code.sum do |line|
93 line_counter += 1
94 "#{indent}#{line_counter}: #{line}\n"
95 end
96 end
但这似乎是对Array#sum方法(https://ruby-doc.org/core-2.5.1/Array.html#method-i-sum)的有效使用。这是我们的代码库有问题还是ActionPack gem中有错误?
以下是相关的,除了我们的config / unicorn.rb:
93 def process_client_with_gc(client)
94 res = process_client_without_gc(client)
95 possibly_run_gc
96 res
97 end
98
99 alias_method :process_client_without_gc, :process_client
100 alias_method :process_client, :process_client_with_gc
答案 0 :(得分:0)
感谢engineersmnky和Tom Lord提供解决方案。
问题是ruby&gt; = 2.4支持Enumerable#sum作为核心的一部分,但rails 3.2也实现了Enumerable#sum。这两种实现是不同的。看起来核心实现优先于rails版本并导致上述错误。
解决方案: