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

时间:2015-08-26 12:21:01

标签: ruby-on-rails ruby-on-rails-4.2 url-helper

我们正在将我们的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

正如所料,仍然有同样的错误。

4 个答案:

答案 0 :(得分:0)

遇到同样的问题,解决方案不是使用:

include Rails.application.routes.url_helpers

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

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

答案 1 :(得分:0)

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

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

url_for_with_secure_option gem。

调用bartt-ssl_requirement

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

如果您在控制台中遇到该错误,因为您使用的是使用URL帮助器的帮助器:

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