ActionController ::参数to_query在升级到Rails 5.0后失败

时间:2017-06-18 17:07:52

标签: ruby-on-rails upgrade

在Rails 4.2.4中,ActionController::Parameters.new(pos: 'xyz', foo: 'bar').to_query返回"foo=bar&pos=xyz"

升级到Rails 5.0后,我明白了:

ArgumentError: wrong number of arguments (given 0, expected 1)
from /Users/Lars/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.rc2/lib/active_support/core_ext/object/to_query.rb:11:in `to_query'
from (irb):7
from /Users/Lars/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/console.rb:65:in `start'
from /Users/Lars/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/console_helper.rb:9:in `start'
from /Users/Lars/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/commands_tasks.rb:78:in `console'
from /Users/Lars/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
from /Users/Lars/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.0.0.rc2/lib/rails/commands.rb:18:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'

to_query的文档明确指出to_query的参数是(仍然)可选:http://api.rubyonrails.org/classes/ActionController/Parameters.html#method-i-to_query

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您正在查看Rails 5.1.1的文档,可以找到5.0.0的文档here

在5.0.0.rc2中,#to_query方法来自here,确实需要一个参数(key)。

这在版本之间发生了变化,自Rails 5.0.3起它不需要参数。我建议只更新你的Rails版本。如果这不是一个选项,这可能对您有用:

Rack::Utils.build_query(ActionController::Parameters.new(pos: 'xyz', foo: 'bar')) # => "pos=xyz&foo=bar"