Rails 4.2 - url helper 引发“ArgumentError:错误数量的参数(3 for 1..2)”

Posted

技术标签:

【中文标题】Rails 4.2 - url helper 引发“ArgumentError:错误数量的参数(3 for 1..2)”【英文标题】:Rails 4.2 - url helper raises "ArgumentError: wrong number of arguments (3 for 1..2)" 【发布时间】:2015-11-20 11:42:06 【问题描述】:

我们正在将 Rails 应用从 4.1 升级到 4.2。

虽然此代码在 4.0 和 4.1 中运行良好:

admin_root_url(:subdomain => 'www')

当我在 4.2.3 上尝试时,我得到了这个错误:

ArgumentError: wrong number of arguments (3 for 1..2)

我在控制台中检查了几次,运行这段代码:

include Rails.application.routes.url_helpers
default_url_options[:host] = "localhost"
root_url

我们的 Gemfile 如下所示:

source 'https://rubygems.org'
ruby "2.1.2"

gem 'rails', '4.2.4'
gem 'rake', "~> 10.2.2"

group :development do
    # Use sqlite3 as the database for Active Record
    gem 'sqlite3'

    # Generates model and controller UML diagrams
  gem 'railroady'
  gem 'bullet'
  gem 'web-console', '~> 2.0'
end

group :staging, :production do
  gem 'pg'
  gem 'rails_12factor'
  gem 'newrelic_rpm'
end

gem 'sass-rails', '~> 4.0.0'
gem 'less-rails-bootstrap'
gem 'uglifier', '>= 1.3.0'
gem 'yui-compressor'
gem 'coffee-rails', '~> 4.0.0'
gem 'therubyracer', platforms: :ruby
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

gem 'simple_form', '~> 3.1.0'
gem "paperclip", "~> 4.2"
gem 'aws-sdk'
gem 'devise', '~> 3.5.2'
gem "cancan"
gem 'friendly_id', '~> 5.0.2'
gem 'pusher', '~> 0.14.5'
gem "configurable_engine", :path => "vendor/gems/configurable_engine"
gem 'backbone-rails'
gem 'marionette-rails'
gem "breadcrumbs_on_rails"
gem 'subdomain-fu', :git => "git://github.com/mbleigh/subdomain-fu.git"
gem 'bitly'
gem 'social-share-button', '~> 0.1.8'
gem 'momentjs-rails', '~> 2.5.0'
gem 'bootstrap3-datetimepicker-rails', '~> 3.0.0'
gem 'rails_autolink'
gem 'jquery-datatables-rails', github: 'rweng/jquery-datatables-rails'
gem "twitter"
gem 'unicorn'
gem 'i18n-js', '>= 3.0.0.rc11'
gem 'sitemap_generator'
gem 'will_paginate-bootstrap'

group :test, :development, :staging do
  gem 'populator'
  gem 'faker'
end

gem "jquery-fileupload-rails"
gem 'rails-timeago', '~> 2.0'
gem 'font-awesome-rails'
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'jquery-minicolors-rails'
gem 'htmlentities', '~> 4.3.2'
gem 'sanitize'
gem 'nokogiri'
gem 'dalli'
gem 'kgio', '~> 2.9.3'
gem 'useragent'
gem "fog", "~>1.20"#, require: "fog/aws/storage"
gem 'asset_sync'
gem 'responders', '~> 2.0'
gem 'rails-deprecated_sanitizer'

================编辑=============== 控制台中的堆栈跟踪:

ArgumentError: wrong number of arguments (3 for 1..2)
    from /home/guy/.rvm/gems/ruby-2.1.2/bundler/gems/subdomain-fu-3752799a02c0/lib/subdomain_fu/url_rewriter.rb:6:in `url_for_with_subdomains'
    from /home/guy/.rvm/gems/ruby-2.1.2/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:282:in `call'
    from /home/guy/.rvm/gems/ruby-2.1.2/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:225:in `call'
    from /home/guy/.rvm/gems/ruby-2.1.2/gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb:347:in `block (2 levels) in define_url_helper'
    from (irb):3
    from /home/guy/.rvm/gems/ruby-2.1.2/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
    from /home/guy/.rvm/gems/ruby-2.1.2/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
    from /home/guy/.rvm/gems/ruby-2.1.2/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /home/guy/.rvm/gems/ruby-2.1.2/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /home/guy/.rvm/gems/ruby-2.1.2/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

================编辑(2)=============== 我深入研究了代码,发现 /gems/actionpack-4.2.4/lib/action_dispatch/routing/route_set.rb (282) 中引发错误的行是:

t._routes.url_for(hash, route_name, url_strategy)

url_rewriter 中的 url_for 函数签名是:

def url_for_with_subdomains(options, path_segments=nil)

看起来我需要更新一些东西,但我不知道是什么。

================编辑(3)=============== 为了确保它与我的routes.rb 文件无关,我将其清理并留下:

MyApp::Application.routes.draw do
  root 'pages#index', :constraints => :subdomain => /www|$^/
end

不出所料,还是一样的错误。

【问题讨论】:

添加生成的stacktrace 您是否尝试过使用更新的哈希语法。 admin_root_url(subdomain: 'www')?然而,对我来说,对“旧”语法的支持已经完全放弃了,这对我来说是新的...... 是的,我试过了。正如我所提到的,即使 url 没有参数,它也会引发相同的错误,如 root_url 可以发你的routes.rb 刚刚做了。与此无关,请看我的edit(2)评论。 【参考方案1】:

遇到同样的问题,解决方法是:

include Rails.application.routes.url_helpers

我没有将 url_helpers 添加到变量中,如下所示:

url_helpers = Rails.application.routes.url_helpers
url_helpers.root_url

【讨论】:

【参考方案2】:

我遇到了完全相同的问题,最后在我的情况下

t._routes.url_for(hash, route_name, url_strategy)

bartt-ssl_requirement gem 调用url_for_with_secure_option

由于我们不再需要 bartt-ssl_requirement gem,因此我们的解决方案是简单地删除 bartt-ssl_requirement gem。

【讨论】:

【参考方案3】:

尝试删除 subdomain-fu 并在需要时使用 Rails4 request.subdomain

【讨论】:

【参考方案4】:

如果您在控制台中收到该错误,因为您使用了一个使用 URL 助手的助手:

class << helper; include Rails.application.routes.url_helpers; end
helper.my_method # \o/

【讨论】:

以上是关于Rails 4.2 - url helper 引发“ArgumentError:错误数量的参数(3 for 1..2)”的主要内容,如果未能解决你的问题,请参考以下文章

如何在 rails URL helper 中传递对象

为啥 Ruby on Rails 的 URL Helper 在我的 URL 中加上句点?

Rails,单击link_to helper后未加载javascript

Ruby / Rails 4.2 - 如果A && B,其中B条件取决于Rails环境

如何将类添加到 image_tag rails helper

从Rails控制台(Rails 3)访问url帮助程序(url等)