【发布时间】:2015-10-13 07:51:48
【问题描述】:
获取肯塔基(KY)州和“默里”城市天气预报的 URI(apikey 已替换)
http://api.wunderground.com/api/apikey/conditions/q/KY/murray.json
我想使用输入框输入状态(例如:KY)和城市,并将它们作为变量传递给 URI,以便我可以根据用户的选择使页面动态,因此尝试将 URI 修改为:
http://api.wunderground.com/api/apikey/conditions/q/#{state}/#{city}.json
我无法将变量作为参数传递给此 api URI 并出现错误
“错误的URI(不是URI?):
我的 weather.erb 文件内容(部分代码):
<% provide(:title, "Weather") %>
<h1>Weather Forecast</h1>
<%=form_tag do%>
<%=label_tag 'Enter State (Eg:KY)'%>
<%=text_field_tag 'state'%><br>
<%=label_tag 'Enter City (Eg:Murray)'%>
<%=text_field_tag 'city'%><br>
<%=submit_tag 'submit '%>
<%end%>
<%
city = yield(:city).strip
state = yield(:state).strip
require 'open-uri'
require 'json'
open('http://api.wunderground.com/api/apikey/conditions/q/#{state}/#{city}.json') do |f|
..........
........
.....
%>
我已经完成了对路由、application_controller 的修改并坚持了这一点。
【问题讨论】:
标签: ruby-on-rails ruby json api