如何在Rails应用程序中使用openweather api显示图标

问题描述 投票:0回答:1

我尝试使用openweather api显示图标。

此功能有效

_else.html.erb
.
.
 <div class="tenki">
    <h2>Tokyo</h2>
    <img src="http://openweathermap.org/img/wn/[email protected]">
.
.

但这只是“ 10d”模式。我当时想显示图标。

所以尝试一下。。

static_pages_controller.rb

.
.
.
 uri = URI.parse("http://api.openweathermap.org/data/2.5/weather?q=Tokyo&appid=#{ENV['OPEN_WEATHER_API_KEY']}")
    json = Net::HTTP.get(uri)
    res = JSON.parse(json)
    @wind = res['wind']['speed']
    @humidity = res['main']['humidity']
    @clouds = res['clouds']['all']
    @icon = res['weather'][0]['icon']
_else.html.erb
.
.
 <div class="tenki">
    <h2>Tokyo</h2>
    <img src="http://openweathermap.org/img/wn/#{@icon}.png">
.
.

但是这行不通..这不是错误,因此错误代码为none。浏览器仅显示破碎的微小图片。

有人修复了吗?请教我。

感谢您阅读本文。

ruby-on-rails openweathermap
1个回答
0
投票

您需要使用ERB标签<%= %>,以使解析器实际评估代码。

© www.soinside.com 2019 - 2024. All rights reserved.