我正在使用 Rails 4,并且在我的一个 View 中遇到了一个奇怪的问题。在我的 Controller 中,我使用 Ruby_Open_Weather_Map 从 OpenWeatherMap.org 获取天气信息。 gem 。数据以 JSON 形式返回,我循环遍历 JSON 并提取 future 5 天的预报信息。奇怪的是,在我的循环之后,显示的是 JSON 数据。
我是 Rails 和 JSON 的新手,这对我来说没有任何意义。我有第二页,上面有更多信息,同样的事情发生了。这是我的 Controller 的样子:
def index
options = { units: "Imperial", APPID: '6eff99d4b460d3a1acddcf1a727a7a45' }
@currentweather = OpenWeather::Current.city("Greenville, SC", options)
@forcastweather = OpenWeather::ForecastDaily.city("Greenville, SC", options)
end
这就是我的观点:
<%= @forcastweather['list'].each do | day | %>
<div class ="row">
<div class="col-xs-5">
<strong><%= Time.at(DateTime.strptime(day['dt'].to_s,'%s')).in_time_zone("Eastern Time (US & Canada)").strftime("%A") %></strong>
</div>
<div class="col-xs-3">
<%= day['temp']['min'] %>
</div>
<div class="col-xs-3">
<%= day['temp']['max'] %>
</div>
<div class="col-xs-2">
<img src="http://openweathermap.org/img/w/<%= day['weather'][0]['icon'] %>.png" alt="<%= day['weather'][0]['main'].humanize %>">
</div>
</div>
<% end %>
为什么 JSON 显示在我的 View 中,我该如何解决?
最佳答案
<%= @forcastweather['list'].each do | day | %>
把=去掉
<% @forcastweather['list'].each do | day | %>
使用 <%=
将告诉 rails 评估此代码,然后呈现结果,因此 rails 评估代码,循环条目,写入 html...然后呈现 @forcastweather['list']
的值.
<%
将告诉 Rails 评估代码,但不从中渲染任何内容。
关于ruby-on-rails - Rails 无意中在 View 中显示 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36334859/