【问题标题】:How to fix bad URI(is not URI? in ruby on rails, passing variables as parameters in URI如何修复坏的URI(不是URI?在ruby on rails中,将变量作为URI中的参数传递)
【发布时间】: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


    【解决方案1】:

    替换

    'http://api.wunderground.com/api/apikey/conditions/q/#{state}/#{city}.json'
    

    "http://api.wunderground.com/api/apikey/conditions/q/#{state}/#{city}.json"
    

    由于单引号不插入变量。

    【讨论】:

      猜你喜欢
      • 2018-04-08
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      相关资源
      最近更新 更多