我无法将变量作为参数传递给此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']
这是我的控制器:
class ForecastsController< ApplicationController的
def show
@weather = Weathers.new(params [:city],params [:state])
结束
端
show.html.erb
每周预测:
<%= @ weathers.day_one%> :<%= @ weathers.day_one_condition%>
高/低:<%= @ weathers.day_one_high%> F:<%= @ weathers.day_one_low%> 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 %>
以下是错误的完整堆栈跟踪
开始GET&#34; /预测?utf8 =%E2%9C%93&amp; city = new + york&amp; state = NY&#34;对于127.0.0.1在2017-10-26 20:09:14 -0400 (0.2ms)SELECT&#34; schema_migrations&#34;。&#34; version&#34; FROM&#34; schema_migrations&#34; ORDER BY&#34; schema_migrations&#34;。&#34;版本&#34; ASC 由ForecastsController处理#显示为HTML 参数:{&#34; utf8&#34; =&gt;&#34;✓&#34;,&#34; city&#34; =&gt;&#34; new york&#34;,&#34; state& #34; = GT;&#34; NY&#34;} 在10ms内完成500内部服务器错误(ActiveRecord:0.0ms)
URI :: InvalidURIError(错误的URI(不是URI?):http://api.wunderground.com/api/apikey/forecast10day/q/new york / NY.json):
app / models / weathers.rb:15:fetch_week_forecast'
app/models/weathers.rb:10:in
初始化&#39;
app / controllers / forecast_controller.rb:36:in new'
app/controllers/forecasts_controller.rb:36:in
show&#39;
延续错误 错误的URI(不是URI?):http://api.wunderground.com/api/apikey/forecast10day/q/new york / 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)
答案 0 :(得分:0)
当您使用HTTParty.get时,您传递的城市和州是“dinamyc”值,因为在这种情况下,城市是“纽约”,网址是用空格创建的,这就是您收到错误的原因:
URI :: InvalidURIError(错误的URI(不是URI?): http://api.wunderground.com/api/apikey/forecast10day/q/new 纽约/ 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