首页 > 解决方案 > 为什么我在 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

标签: ruby-on-railsrubyheroku

解决方案


您对此方法的调用:

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 的方式”。


推荐阅读