如何在 rails 上修复 ruby 中的错误 URI(不是 URI?):URI::InvalidURIError

How to fix bad URI(is not URI?) in ruby on rails :URI::InvalidURIError

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

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

型号:weathers.rb class天气

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
定义显示
@weather = Weathers.new(params[:city], params[:state])
结束
结束

show.html.erb 每周预测:
<%=@weathers.day_one %> : <%= @weathers.day_one_condition %>
High/Low:<%=@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 %>

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

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

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

app/models/weathers.rb:15:in fetch_week_forecast' app/models/weathers.rb:10:in初始化' app/controllers/forecasts_controller.rb:36:在 new' app/controllers/forecasts_controller.rb:36:inshow'

错误继续 错误的 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)

当您使用 HTTParty.get 时,您传递的城市和州是 "dynamic" 值,因为在本例中城市是 "new york",正在创建 url一个空格,这就是你得到错误的原因:

URI::InvalidURIError (bad URI(is not URI?): http://api.wunderground.com/api/apikey/forecast10day/q/new york/NY.json)

您可以使用 URI.encode(url) 这样,空格将是 "converted" 到 %20,例如:

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

这是一个 "valid" url 的工作。所以你可以稍微调整一下你的 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