Ruby on rails 为什么我在Heroku中得到一个ActionView::Template::Error(提供的位置为零。Can';t build URI)错误,而不是在开发中?

Ruby on rails 为什么我在Heroku中得到一个ActionView::Template::Error(提供的位置为零。Can';t build URI)错误,而不是在开发中?,ruby-on-rails,ruby,heroku,Ruby On Rails,Ruby,Heroku,我正在构建一个简单的应用程序,它使用OpenWeatherMapAPI并返回一些天气参数和与当前天气相对应的gif。用户可以搜索城市的当前天气 本地一切正常,但我已经部署到Heroku,当我搜索一个城市时,它会抛出一个错误。Heroku日志显示ActionView::Template::Error(提供的位置为零。无法生成URI下面是我的代码 错误发生在此行: 视图/current_weather.html.erb <div class="page-wrapper"> <h

我正在构建一个简单的应用程序,它使用OpenWeatherMapAPI并返回一些天气参数和与当前天气相对应的gif。用户可以搜索城市的当前天气

本地一切正常,但我已经部署到Heroku,当我搜索一个城市时,它会抛出一个错误。Heroku日志显示
ActionView::Template::Error(提供的位置为零。无法生成URI
下面是我的代码

错误发生在此行:

视图/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>
facades/Forecast_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
服务/开放天气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\u gif\u url
GIFS.每个do |键、值|
如果值[:代码]。包括?@forecast\u facade.weather\u代码
返回值[:URL]。示例
结束
结束
结束
返回<代码> nIL/COD>。也就是说,在<代码> GIFS 哈希中搜索条目实际上没有找到结果。请考虑<代码> @ ValueStsFutal.WeiTythCudio<代码>的值,以及为什么不能在Value[[代码] ]中找到它的值。。添加一些代码来处理在
GIFS
中找不到条目的情况可能是个好主意


另一方面,我可能建议使用,而不是迭代
GIFS
并手动返回。这将是“惯用Ruby”,或“Ruby方式”。您对该方法的调用:

def find\u gif\u url
GIFS.每个do |键、值|
如果值[:代码]。包括?@forecast\u facade.weather\u代码
返回值[:URL]。示例
结束
结束
结束
返回<代码> nIL/COD>。也就是说,在<代码> GIFS 哈希中搜索条目实际上没有找到结果。请考虑<代码> @ ValueStsFutal.WeiTythCudio<代码>的值,以及为什么不能在Value[[代码] ]中找到它的值。。添加一些代码来处理在
GIFS
中找不到条目的情况可能是个好主意


另一方面,我可能建议使用,而不是迭代
GIFS
并手动返回。这将是“惯用Ruby”,或“Ruby方式”.

Ruby错误消息通常包括发生错误的确切文件名和行号。请添加该信息。此外,您检查
@city.nil?
,如果
@city
为空字符串会发生什么情况?您可能希望改为检查
.blank?
。是否在
产品中设置了
令牌
environment?@spickermann我已经在问题发生错误的地方添加了一行代码。
TOKEN
设置在加密的凭证文件中。什么是
find\u gif\u url
?它在何处以及如何定义?@spickermann它是在助手类中定义的。我现在已经在问题中添加了文件代码。我感觉像
@forecasts_facade.weather_code
正在返回一个意外的代码–可能代码列表不完整,或者在某些情况下可能为空。您是否检查了实际返回的代码?此外,我建议使用一个备用图标以防万一。Ruby错误消息通常包括错误代码所在的确切文件名和行号urred.请添加该信息。此外,您检查
@city.nil?
,如果
@city
为空字符串会发生什么情况?您可能需要检查
.blank?
。您的
生产环境中是否设置了
令牌
。@spickermann我添加了问题出错的代码行。
TOKEN
是在加密的凭证文件中设置的。什么是
find\u gif\u url
?它在哪里以及如何定义?@spickermann它是在一个助手类中定义的。我现在已经将文件代码添加到问题中。我感觉像是
@forecast\u facade。weather\u code
返回了一个意外的代码–可能代码列表中的代码不正确完成,或者在某些情况下它可能是空的。你检查过实际返回的代码吗?此外,我建议设置一个回退图标以防万一。为什么它在开发中运行良好,而在生产中运行不好?@Steve我对你的应用程序了解不够,无法给你一个超出猜测的答案。乍一看,我我们将检查生产中是否正确设置了
标记。
ForecastsFacade
对这里的每个人来说都是一个完全的黑匣子,我们不知道它将如何处理
nil
标记值。我们只是无法知道
@forecast\u facade
的可能值是什么,以及您遇到的原因这个问题是因为if语句条件(
if value[:code])对于
GIFS
中的任何值,include?@forecast\u facade.weather\u code
)都不是真的。谢谢你的回复:)我不确定我怎么会错过它。我添加了我的服务类和门面,让您对应用程序有更多的了解。目前,正在我的加密凭据文件中设置
令牌
。它不是专门为开发或生产而设置的-就像一个键
openweather\u key
为什么它在开发中运行良好而不是在生产中运行?@Steve我对您的应用程序了解不够,无法为您提供超出猜测的答案。乍一看,我会检查
令牌
是否在生产中正确设置
ForecastsFacade对这里的每个人来说都是一个完整的黑盒子,我们不知道它如何处理
nil
的标记值。我们无法知道
@forecast\u facade
的可能值是什么,您遇到问题的原因是因为if语句条件(
if value[:code])include?@forecast\u facade.weather\u code
)对于
GIFS
中的任何值都不是真的。谢谢您的回复:)不确定
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
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