![](/img/trans.png)
[英]Why am I getting this error on heroku but not in development in my rails app?
[英]Why am I getting a name error in my Rails controller whilst initializing my facade class?
我正在創建一個簡單的Rails應用程序,該應用程序使用Open Weather Map API,並返回各種天氣參數。 我的API調用是在服務類中處理的,我正在嘗試遵循外觀模式並在PORO中處理參數數據。 我已經在下面的代碼。 我收到一個NameError. undefined local variable or method
NameError. undefined local variable or method
city'`知道我要去哪里了嗎?
app / services / 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
app / facades / forecasts_facade.rb
class ForecastsFacade
TOKEN = Rails.application.credentials.openweather_key
def initialize(city, forecast, temperature, weather_code, description, wind, humidity)
@city = city
@forecast = forecast
@temperature = temperature
@weather_code = weather_code
@description = description
@wind = wind
@humidity = humidity
end
def city
@city = params[:q]
end
def forecast
if @city.nil?
@forecast = {}
else
@forecast = OpenWeatherApi.new(@city, TOKEN).my_location_forecast
end
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
end
Forecast_controller.rb
class ForecastsController < ApplicationController
def current_weather
@forecasts_facade = ForecastsFacade.new(city, forecast, temperature, weather_code, description, wind, humidity)
end
end
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 ForecastsFacade.forecast.dig('cod').to_i == 404 %>
<p>Please use a valid city name!</p>
<% elsif ForecastsFacade.forecast.dig('cod').to_i == 400 %>
<p>Please type in a city name!</p>
<% elsif ForecastsFacade.forecast == {} %>
<% else %>
<p class="weather-description"><%= "#{ForecastsFacade.city.capitalize}: #{ForecastsFacade.description}" %></p>
<div class="gif-container"><%= image_tag(find_gif_url, class: "gif") %>
<span class="temperature weather-attribute"><%= "#{ForecastsFacade.temperature}°C" %></span>
<span class="wind weather-attribute"><%= "wind:#{(ForecastsFacade.wind * 3.6).to_i}km/h" %></span> <!-- converts to km/hr -->
<span class="humidity weather-attribute"><%= "humidity:#{ForecastsFacade.humidity}%" %></span>
</div>
<% end %>
</div>
從您的代碼中,我可以看到在current_weather.html.erb
頁面上。 你寫:
<% if ForecastsFacade.forecast.dig('cod').to_i == 404 %>
<p>Please use a valid city name!</p>
<% elsif ForecastsFacade.forecast.dig('cod').to_i == 400 %>
<p>Please type in a city name!</p>
<% elsif ForecastsFacade.forecast == {} %>
<% else %>
<p class="weather-description"><%= "#{ForecastsFacade.city.capitalize}: #{ForecastsFacade.description}" %></p>
<div class="gif-container"><%= image_tag(find_gif_url, class: "gif") %>
<span class="temperature weather-attribute"><%= "#{ForecastsFacade.temperature}°C" %></span>
<span class="wind weather-attribute"><%= "wind:#{(ForecastsFacade.wind * 3.6).to_i}km/h" %></span> <!-- converts to km/hr -->
<span class="humidity weather-attribute"><%= "humidity:#{ForecastsFacade.humidity}%" %></span>
</div>
<% end %>
這里ForecastsFacade
是一個類。 forecast
是一種實例方法。 因此,在調用函數時不會調用initialized
。
嘗試使用ajax請求,並在@forecasts_facade
中使用js.erb
來創建局部文件,這將有助於您實現功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.