【问题标题】:How to fix bad URI(is not URI?) in ruby on rails :URI::InvalidURIError如何在 ruby​​ on rails 中修复错误的 URI(不是 URI?):URI::InvalidURIError
【发布时间】:2018-04-08 07:56:48
【问题描述】:

我无法将变量作为参数传递给这个 api:wounderground。

我想使用输入框将输入作为城市和州,并将它们作为变量传递给 URI,以便用户可以获取他们的天气预报。但是,我无法将变量作为参数传递给 api,并且出现错误的 URI(不是 URI?)或 URI::InvalidURIError。有人可以告诉我如何解决它并告诉我为什么会出现此错误。如果您需要更多信息,请告诉我。谢谢!

型号:weathers.rb 天气类

attr_accessor :temperature, :city, :state, :icon, :weekday_name, 
:chance_of_rain, :chance_of_snow, :uvi, :tomorrow, :tomorrow_condition, 
:tomorrow_icon, :day_one, `enter code here`:day_one_condition, 
:day_one_high, :day_one_low, :day_two, :day_two_condition, 
:day_two_high, :day_two_low

def initialize(city, state)
@city = city
@state = state

week_weather_hash = fetch_week_forecast(city, state)
week_forecast(week_weather_hash)
end

def fetch_week_forecast(city, state)

HTTParty.get("http://api.wunderground.com/api/apikey/forecast10day/q/#
{city}/#{state}.json")
end

def week_forecast(week_weather_hash)
weekly_forecast_response = 
week_weather_hash.parsed_response['response']['forecast']
self.day_one = weekly_forecast_response['txt_forecast']['forecastdays']
['forecastday'][0]['title']
self.day_one_condition = weekly_forecast_response['txt_forecast']
['forecastdays']`enter code here`['forecastday'][0]['fcttext']

self.day_one_high = weekly_forecast_response['simpleforecast']
['forecastdays']['forecastday'][0]['high']['fahrenheit']
self.day_one_low = weekly_forecast_response['simpleforecast']
['forecastdays']['forecastday'][0]['low']['fahrenheit']

self.day_two = weekly_forecast_response['txt_forecast']['forecastdays']
['forecastday'][2]['title']
self.day_two_condition = weekly_forecast_response['txt_forecast']
['forecastdays']`enter code here`['forecastday'][2]['fcttext']

self.day_two_high = weekly_forecast_response['simpleforecast']
['forecastdays']`enter code here`['forecastday'][1]['high']
['fahrenheit']
self.day_two_low = weekly_forecast_response['simpleforecast']
['forecastdays']['forecastday'][1]['low']['fahrenheit']

这是我的控制器: 类 ForecastsController 默认显示
@weather = Weathers.new(params[:city], params[:state])
结束
结束

show.html.erb 每周预测:
:
高/低:F:F

<br>
<%=@weathers.day_two %> : <%= @weathers.day_two_condition %>
<br>
High/Low: <%=@weathers.day_two_high %>F : <%=@weathers.day_two_low 
%>F 

**我的 index.html.erb 和新表单 **

<%= form_tag("/forecast", method: "get", class: "form-inline") do %> 
<p class = "city-input">
<%= label_tag :City %>
<%= text_field_tag :city,  nil, class: "form-control", placeholder: 
"City Name" %>
</p>

<p class= "state-input">
<%= label_tag :State %>
<%= select_tag :state, options_for_select(us_states, "NY"), class: 
"form-control" %>
</p>

<p class= "submit">
<%= submit_tag 'Search', name: nil, class:"btn btn-primary" %>
</p>
<% end %>

以下是错误的完整堆栈跟踪

于 2017-10-26 20:09:14 -0400 开始 GET "/forecast?utf8=%E2%9C%93&city=new+york&state=NY" for 127.0.0.1 (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC 由 ForecastsController#show 处理为 HTML 参数:{"utf8"=>"✓", "city"=>"new york", "state"=>"NY"} 在 10 毫秒内完成 500 个内部服务器错误(ActiveRecord:0.0 毫秒)

URI::InvalidURIError(错误的 URI(不是 URI?):http://api.wunderground.com/api/apikey/forecast10day/q/newyork/NY.json):

app/models/weathers.rb:15:in fetch_week_forecast' app/models/weathers.rb:10:ininitialize' app/controllers/forecasts_controller.rb:36:in new' app/controllers/forecasts_controller.rb:36:inshow'

错误继续 错误的 URI(不是 URI?):http://api.wunderground.com/api/apikey/forecast10day/q/newyork/NY.json

提取的源代码(第 15 行附近): 13 14 15 16 17 18

def fetch_week_forecast(city, state)
    HTTParty.get("http://api.wunderground.com/api/apikey/forecast10day/q/#{city}/#{state}.json")
end

def week_forecast(week_weather_hash)

【问题讨论】:

  • 请通过删除enter code here 并正确缩进来清理您的代码。我让你开始编辑,但还有更多工作要做。
  • 我已删除“在此处输入代码”
  • 发布错误的完整堆栈跟踪。
  • 好的,我正赶回家发帖。谢谢!
  • 我用错误的完整堆栈跟踪更新了问题。谢谢!

标签: ruby-on-rails weather-api


【解决方案1】:

当您使用 HTTParty.get 时,您传递的是“动态”值的城市和州,在这种情况下,城市是“纽约”,正在创建带有空格的 url,这就是您收到错误的原因:

URI::InvalidURIError(错误的 URI(不是 URI?): http://api.wunderground.com/api/apikey/forecast10day/q/new york/NY.json)

您可以使用URI.encode(url),这样,空白将“转换”为%20,例如:

http://api.wunderground.com/api/apikey/forecast10day/q/new%20york/NY.json

这是一个“有效”的网址。所以你可以稍微调整一下你的 fetch_week_forecast 方法,比如:

def fetch_week_forecast(city, state)
  HTTParty.get(URI.encode("http://api.wunderground.com/api/apikey/forecast10day/q/#{city}/#{state}.json"))
end

【讨论】:

  • 非常感谢塞巴斯蒂安!!! - URI.encode 将空格转换为.... 现在,用户可以在进入他们的城市和州时获取天气数据:)
猜你喜欢
  • 2012-02-23
  • 2011-07-19
  • 2012-04-09
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
相关资源
最近更新 更多