ruby-on-rails - 为什么我在 Heroku 中收到 ActionView::Template::Error (Nil location provided. Can't build URI) 错误,但在开发中却没有?
问题描述
我正在构建一个使用 Open Weather Map API 并返回一些天气参数和对应于当前天气的 gif 的简单应用程序。用户可以搜索一个城市的当前天气。
在本地一切正常,但我已经部署到 Heroku,当我搜索一个城市时,它会抛出一个错误。Heroku 日志显示ActionView::Template::Error (Nil location provided. Can't build URI
我的代码如下。
错误发生在这一行:<div class="gif-container"><%= image_tag(find_gif_url, class: "gif") %>
意见/current_weather.html.erb
<div class="page-wrapper">
<h1 class="title">The weather in GIFs</h1>
<div class="search">
<%= form_tag(current_weather_forecasts_path, method: :get) do %>
<%= text_field_tag :q, nil, placeholder: "Enter a city", class: "search-field" %>
<%= button_tag type: 'submit', class: "search-button" do %>
<i class="fas fa-search"></i>
<% end %>
<% end %>
</div>
<% if @forecasts_facade.invalid_city? %>
<p>Please use a valid city name!</p>
<% elsif @forecasts_facade.missing_city? %>
<p>Please type in a city name!</p>
<% elsif @forecasts_facade.forecast == {} %>
<% else %>
<p class="weather-description"><%= "#{@city.capitalize}: #{@forecasts_facade.description}" %></p>
<div class="gif-container"><%= image_tag(find_gif_url, class: "gif") %>
<span class="temperature weather-attribute"><%= "#{@forecasts_facade.temperature}°C" %></span>
<span class="wind weather-attribute"><%= "wind:#{(@forecasts_facade.wind * 3.6).to_i}km/h" %></span> <!-- converts to km/hr -->
<span class="humidity weather-attribute"><%= "humidity:#{@forecasts_facade.humidity}%" %></span>
</div>
<% end %>
</div>
预测控制器.rb
class ForecastsController < ApplicationController
TOKEN = Rails.application.credentials.openweather_key
def current_weather
@city = params[:q]
if @city.nil?
@forecast = {}
else
@forecast = OpenWeatherApi.new(@city, TOKEN).my_location_forecast
end
@forecasts_facade = ForecastsFacade.new(@forecast)
end
end
助手/forecasts_helper.rb
module ForecastsHelper
GIFS = {
thunder:
{codes: [200, 201, 202, 210, 211, 212, 221, 230, 231, 232],
urls: %w(
https://media.giphy.com/media/26uf5HjasTtxtNCqQ/giphy.gif
https://media.giphy.com/media/vS09bj1KrXwje/giphy.gif
)},
light_rain:
{codes: [300, 301, 302, 310, 311, 312, 313, 314, 321, 500, 501, 520, 521],
urls: %w(
https://media.giphy.com/media/xT9GEz2CeU9uaI2KZi/giphy.gif
https://media.giphy.com/media/k28n1OPefBEeQ/giphy.gif
https://media.giphy.com/media/H1eu9Vw957Rfi/giphy.gif
https://media.giphy.com/media/Jyeo8wBpdwpry/giphy.gif
https://media.giphy.com/media/14d8cRr25KPxbG/giphy.gif
)},
heavy_rain:
{codes: [502, 503, 504, 522, 531, 511],
urls: %w(
https://media.giphy.com/media/1Yfxps0AHRYBR2tK2G/giphy.gif
https://media.giphy.com/media/7zSIC0roM238CVTS4u/giphy.gif
https://media.giphy.com/media/l0Expn6D0D3g9NExq/giphy.gif
)},
}
def find_gif_url
GIFS.each do |key, value|
if value[:codes].include? @forecasts_facade.weather_code
return value[:urls].sample
end
end
end
end
外墙/forecasts_facade.rb
class ForecastsFacade
attr_accessor *%w(
forecast
).freeze
def initialize(forecast)
@forecast = forecast
end
def temperature
@temperature = forecast.dig('main', 'temp').to_i - 273
end
def weather_code
@weather_code = forecast.dig('weather', 0, 'id').to_i
end
def description
@description = forecast.dig('weather', 0, 'description')
end
def wind
@wind = forecast.dig('wind', 'speed').to_i
end
def humidity
@humidity = forecast.dig('main', 'humidity')
end
def invalid_city?
forecast.dig('cod').to_i == 404
end
def missing_city?
forecast.dig('cod').to_i == 400
end
end
服务/open_weather_api.rb
class OpenWeatherApi
include HTTParty
base_uri "http://api.openweathermap.org"
def initialize(city, appid)
@options = { query: { q: city, APPID: appid } }
end
def my_location_forecast
self.class.get("/data/2.5/weather", @options)
end
end
解决方案
您对此方法的调用:
def find_gif_url
GIFS.each do |key, value|
if value[:codes].include? @forecasts_facade.weather_code
return value[:urls].sample
end
end
end
正在回归nil
。也就是说,您在GIFS
哈希中搜索条目实际上并没有找到结果。考虑 的值@forecasts_facade.weather_code
以及为什么在 的值中找不到它value[:codes]
。GIFS
添加一些代码来处理找不到条目的情况可能是一个好主意。
顺便说一句,我可能会建议使用而不是手动Enumerable#find
迭代和返回。GIFS
那将是“惯用的 Ruby”,或“Ruby 的方式”。
推荐阅读
- google-cloud-bigtable - HappyBase Table.cells 不返回任何结果
- powershell - 将邮箱用户交换到 PowerBi
- node.js - 如何在 NodeJs 中使用 util.promisify 和 .bind 函数?
- sapui5 - 将 Fiori 元素应用程序插入自由式应用程序
- c - 使用此语法在 flex/bison 中出现语法错误
- ring - ring monoid,任何人都可以举一个不是另一个的例子吗
- python - 为什么这段代码无法将复数转换为浮点数?或者我得到无效的语法
- java - 删除时违反数据模型的参照完整性约束
- javascript - CSS动画功能在IE浏览器中不起作用
- excel - 限制用户表单中的文本框接受 mm/dd/yy 格式的日期