【问题标题】:How do I access my Rails helper method in my view? "Nil location provided. Can't build URI"如何在我的视图中访问我的 Rails 辅助方法? “提供的位置为零。无法构建 URI”
【发布时间】:2019-06-08 19:59:05
【问题描述】:

我正在尝试创建一个应用程序,该应用程序根据某个位置的当前天气情况返回一个 gif 网址。我正在使用开放天气 api,并将我的 url 存储在模块中的哈希中,并创建一个方法,该方法根据从 api 调用返回的天气代码选择 gif url。例如,代码 321 对应于“小雨”,然后应该返回一个描绘小雨的 gif。

当我运行我的代码时,我得到一个参数错误Nil location provided. Can't build URI 我不确定我是否以正确的方式访问辅助方法。 API 调用工作正常,我可以将@weather_code 返回到我的视图中,没有问题。知道是什么原因造成的吗?这是我的代码:

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
          https://media.giphy.com/media/2pUAUd0cFntny/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

)},
    heavy_rain:
      {codes: [502, 503, 504, 522, 531, 511],
       urls: %w(
          https://media.giphy.com/media/1Yfxps0AHRYBR2tK2G/giphy.gif
          https://media.giphy.com/media/hk6czgfmwVJS0/giphy.gif
          https://media.giphy.com/media/26BGD4XaoPO3zTz9K/giphy.gif
)}
  }

  def find_gif_url
    GIFS.each do |key, value|
      if value[:codes].include? @weather_code
        value[:urls].sample
      end
    end
  end
end

forecasts_controller.rb

class ForecastsController < ApplicationController
  def current_weather
    @token = Rails.application.credentials.openweather_key
    @city = params[:q]
    if @city.nil?
      @forecast = {}
    else
      @forecast = OpenWeatherApi.new(@city, @token).my_location_forecast
    end
    @temperature = @forecast.dig('main', 'temp').to_i - 273
    @weather_code = @forecast.dig('weather', 0, 'id').to_i
  end
end

current_weather.html.erb

<%= form_tag(current_weather_forecasts_path, method: :get) do %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
<% end %><br>

<%= image_tag(find_gif_url, class: "gif") %>

【问题讨论】:

  • 鉴于错误消息Nil location provided. Can't build URI 我猜OpenWeatherApi.new(@city, @token) 失败了。 @city 设置了吗?您是否将 params[:q] 传递给该控制器?
  • @spickermann 我不认为它失败了。如果我有 &lt;%= @weather_code %&gt; 在我看来,我会得到我搜索的相应城市的预期代码。
  • @spickermann 是的,我通过了params[:q]。我已将代码添加到问题中。

标签: ruby-on-rails ruby


【解决方案1】:

您正在遍历所有GIFS,但在有匹配代码时不返回。改变

def find_gif_url
  GIFS.each do |key, value|
    if value[:codes].include? @weather_code
      value[:urls].sample
    end
  end
end

def find_gif_url
  GIFS.each do |key, value|
    if value[:codes].include? @weather_code
      return value[:urls].sample
    end
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    相关资源
    最近更新 更多