我正在尝试将原点和目的地的坐标发布到bing api以获取路线。但我无法发送数据。
def get_info_from_bing(params)
base_url="http://dev.virtualearth.net/REST/v1/Routes/"
query_params = "?" + {
"wayPoint.1" => params[:origin],
"waypoint.2" => params[:destination],
"dateTime" => params[:time] || Time.now.strftime("%H:%M"),
"timeType" => "Arrival",
"key" => ENV['BING_KEY']
}.map {|k,v| "#{k}=#{CGI.escape(v)}"}*"&"
modes=%w{driving walking transit}
错误:
NoMethodError - undefined method `gsub' for nil:NilClass:
/Users/anirvan/.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/cgi/util.rb:7:in `escape'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:15:in `block in get_info_from_bing'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:15:in `each'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:15:in `map'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:15:in `get_info_from_bing'
/Users/anirvan/codeforamerica-transpochoices-1ccac48/choices.rb:120:in `block in <top (required)>'
/Users/anirvan/.rvm/gems/ruby-1.9.2-p318/gems/sinatra-1.3.1/lib/sinatra/base.rb:1212:in `call'
/Users/anirvan/.rvm/gems/ruby-1.9.2-p318/gems/sinatra-1.3.1/lib/sinatra/base.rb:1212:in `block in compile!'
/Users/anirvan/.rvm/gems/ruby-1.9.2-p318/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `[]'
/Users/anirvan/.rvm/gems/ruby-1.9.2-p318/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `block (3 levels) in route!'
我是sinatra和ruby的新手。有谁知道问题是什么?
答案 0 :(得分:1)
使用params.inspect
打印param
数组的内容。其中一个值肯定是nil
,这就是CGI.escape(v)
抛出异常的原因。
如果您可以修复nil
值,请使用以下行作为解决方法:
...
}.map {|k,v| "#{k}=#{CGI.escape(v||'')}"}*"&"
...