gpt4 book ai didi

ruby-on-rails - HTTParty 未定义方法 '+' 为 nil :NilClass

转载 作者:行者123 更新时间:2023-12-02 00:55:07 26 4
gpt4 key购买 nike

使用 HTTPParty gem 连接到天气 API 我有以下方法:

def weather_search
@city_name = params[:city_name]
url = URI.encode("api.openweathermap.org/data/2.5/weather?q=#{@city_name}&APPID=#{API_TOKEN}")
@search_result = HTTParty.get(url)
end

gem 已安装。

使用 postman ,我可以通过以下 url 毫无问题地访问 API

api.openweathermap.org/data/2.5/weather?q=banfield&APPID=xxxxxxxxxxxxxxx

但是在我的 rails 4 应用程序中,相同的 url 会抛出错误

undefined method '+' for nil:NilClass 在我声明的那一行

@search_result = HTTParty.get(url)

我找到了 thisthis other关于这个主题的帖子,但两者都没有帮助。

堆栈跟踪:

Started POST "/search_city_weather" for 127.0.0.1 at 2016-03-30 23:50:13 +0100
Processing by WeatherChecksController#weather_search as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "city_name"=>"banfield", "commit"=>"Search"}
api.openweathermap.org/data/2.5/weather?q=banfield&APPID=xxxxxxxxxxxxxxxxxx
Completed 500 Internal Server Error in 11ms
NoMethodError (undefined method `+' for nil:NilClass):
app/controllers/weather_checks_controller.rb:24:in `weather_search'
Rendered /home/xxxx/.rvm/gems/ruby-2.2.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.3ms)
Rendered /home/xxxx/.rvm/gems/ruby-2.2.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.1ms)
Rendered /home/xxxx/.rvm/gems/ruby-2.2.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.3ms)
Rendered /home/xxxx/.rvm/gems/ruby-2.2.2/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (22.6ms)

任何帮助将不胜感激。

最佳答案

正如@tadman 所写,您应该为 httparty 提供一个有效的 URI,所以:

顺便说一句,在 0.14.0 之后的版本中,在这些情况下,Httparty 将返回此错误:URI::InvalidURIError:错误的 URI(不是 URI?):

比这更清楚

undefined method '+' for nil:NilClass

关于ruby-on-rails - HTTParty 未定义方法 '+' 为 nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36320893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com