Rails - 突然,redirect_to总是给出错误的参数数量(0表示1)

时间:2016-04-13 15:08:58

标签: ruby-on-rails

突然之间,我的Rails应用上的所有redirect_to次调用都返回了此wrong number of arguments (0 for 1)错误。我无法确定原因。

即使是简单的电话,如

def show
   redirect_to root_path
end

# routes
root to: "home#index"

......在回溯中没有任何迹象,至少我可以指出。

我已经尝试过了,基于此处回答的其他问题:

  • 更新turbolinks gem
  • 更新所有宝石
  • 删除active_admin
  • 删除设计
  • 删除专家
  • 调用路径助手,url助手,绝对路径,相对路径,传递参数,不传递参数
  • 更新actionpack
  • 更新Rails

在所有其他答案中,问题似乎是古老的宝石或拼写错误,但在我的情况下,所有这些都只是停止了工作,即使那些已经好几个月并且未受影响的那些。

跟踪从应用程序的任何位置始终完全相同:

    Redirected to
    Completed 500 Internal Server Error in 177ms

    ArgumentError - wrong number of arguments (0 for 1):
      actionpack (4.2.0) lib/action_controller/metal/redirecting.rb:76:in `redirect_to'
      actionpack (4.2.0) lib/action_controller/metal/flash.rb:57:in `redirect_to'
      actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:61:in `block in redirect_to'
      activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
      activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
      activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
      actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:60:in `redirect_to'
      app/controllers/ads_controller.rb:93:in `block (2 levels) in create'
      actionpack (4.2.0) lib/action_controller/metal/mime_responds.rb:216:in `respond_to'
      app/controllers/ads_controller.rb:92:in `create'
      actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
      actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
      actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
      actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
      activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
      activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
      activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
      activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
      activesupport (4.2.0) lib/active_support/callbacks.rb:151:in `block in halting_and_conditional'
      activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
      activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
      activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
      activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
      activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
      activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
      activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
      activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
      actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
      actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
      actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
      activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
      activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
      activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
      actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
      actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
      activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
      actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
      actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
      actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
      actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
      actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
      actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
      actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
      actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
      actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
      actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
      omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
      omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      bullet (5.0.0) lib/bullet/rack.rb:12:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/rack/developer_mode.rb:48:in `traced_call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
      warden (1.2.3) lib/warden/manager.rb:34:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      rack (1.6.4) lib/rack/etag.rb:24:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      rack (1.6.4) lib/rack/head.rb:13:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
      rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
      activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
      activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
      activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
      actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
      better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
      better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      web-console (2.1.3) lib/web_console/middleware.rb:37:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
      railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
      activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
      activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
      activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
      railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      rack (1.6.4) lib/rack/runtime.rb:18:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      rack (1.6.4) lib/rack/lock.rb:17:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      railties (4.2.0) lib/rails/engine.rb:518:in `call'
      railties (4.2.0) lib/rails/application.rb:164:in `call'
      newrelic_rpm (3.12.1.298) lib/new_relic/agent/instrumentation/middleware_tracing.rb:67:in `call'
      rack (1.6.4) lib/rack/content_length.rb:15:in `call'
      puma (2.11.3) lib/puma/server.rb:507:in `handle_request'
      puma (2.11.3) lib/puma/server.rb:375:in `process_client'
      puma (2.11.3) lib/puma/server.rb:262:in `block in run'
      puma (2.11.3) lib/puma/thread_pool.rb:104:in `block in spawn_thread'

通过这个版本的Rails源代码,这是给出麻烦的方法:

def redirect_to(options = {}, response_status = {}) #:doc:
  raise ActionControllerError.new("Cannot redirect to nil!") unless options
  raise ActionControllerError.new("Cannot redirect to a parameter hash!") if options.is_a?(ActionController::Parameters)
  raise AbstractController::DoubleRenderError if response_body
  self.status        = _extract_redirect_to_status(options, response_status)
  self.location      = _compute_redirect_to_location(request, options)
  self.response_body = "<html><body>You are being <a href=\"#{ERB::Util.unwrapped_html_escape(location)}\">redirected</a>.</body></html>"
end

根据我的有限知识,我有三个嫌疑人:

  1. HAML,因为呼叫是针对最后一行的ERB,是追溯中的ERB(但我不觉得它在这里)
  2. 请求中的某些内容已停止由某些代码发送,在location var。
  3. 中提供了问题
  4. flash消息中的某些内容?我安装了Toasts,但它已经工作了好几周,即使没有设置flash消息也会发生错误。这是主要的嫌疑人。
  5. 但是如何通过更多细节解决或调试此问题?

    编辑:从backtrace添加redirect_to行 在这种特殊情况下给出错误的问题是

    if @ad.save
      UserMailer.admin_event_email('create_ad', @ad).deliver_later
      redirect_to ad_created_path, success: I18n.t('The ad is poster and a moderator will aprove it shortly')
    end
    

    但它总是发生在redirect_to行。

2 个答案:

答案 0 :(得分:0)

更改行

redirect_to ad_created_path, success: I18n.t('The ad is poster and a moderator will aprove it shortly')

为:

redirect_to ad_created_path, :flash => { success: I18n.t('The ad is poster and a moderator will aprove it shortly') }

修改

将turbo-links更新为verison 2.5.3

gem 'turbolinks', '~> 2.5.3'

编辑2:

在这一行设置一个断点:

=> 76:       byebug
=> 77:       self.response_body = "<html><body>You are being <a href=\"#{ERB::Util.unwrapped_html_escape(location)}\">redirected</a>.</body></html>"

"<html><body>You are being <a href=\"#{ERB::Util.unwrapped_html_escape(location)}\">redirected</a>.</body></html>"的结果是什么?

答案 1 :(得分:0)

所以,经过长时间的工作,我发现至少部分罪魁祸首:包括ApplicationHelper 进入控制器。

在包含ApplicationHelper的所有控制器中,都会发生此错误。现在,这就是我所做的:

  • 从控制器中删除了ApplicationHelper
  • 将对辅助方法的调用更改为控制器中的view_context.my_helper

现在,我将尝试找到导致问题的帮助器,并在我找到时更新此答案。