[英]Why am I getting a name error in my Rails controller whilst initializing my facade class?
I'm creating a simple Rails app that consumes the Open Weather Map API and returns various weather parameters. 我正在创建一个简单的Rails应用程序,该应用程序使用Open Weather Map API,并返回各种天气参数。 My API call is handled in a service class and I'm trying to follow the facade pattern and deal with the parameter data in a PORO.
我的API调用是在服务类中处理的,我正在尝试遵循外观模式并在PORO中处理参数数据。 The code I have already is below.
我已经在下面的代码。 I am getting a
NameError. undefined local variable or method
我收到一个
NameError. undefined local variable or method
NameError. undefined local variable or method
city'` Any idea where I am going wrong? NameError. undefined local variable or method
city'`知道我要去哪里了吗?
app/services/open_weather_api.rb 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 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
forecasts_controller.rb 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 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>
From your code what I can see is that on the page current_weather.html.erb
. 从您的代码中,我可以看到在
current_weather.html.erb
页面上。 You have written: 你写:
<% 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 %>
Here ForecastsFacade
is a class. 这里
ForecastsFacade
是一个类。 forecast
is an instance method. forecast
是一种实例方法。 So, initialized
is not called when you're calling the function. 因此,在调用函数时不会调用
initialized
。
Try to use ajax request and use @forecasts_facade
in the js.erb
to create a partial which will help you in achieving the functionality. 尝试使用ajax请求,并在
@forecasts_facade
中使用js.erb
来创建局部文件,这将有助于您实现功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.