简体   繁体   English

为什么在初始化外观类时在Rails控制器中出现名称错误?

[英]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.

相关问题 为什么我在heroku上却出现此错误,而在我的Rails应用程序中却没有开发此错误? - Why am I getting this error on heroku but not in development in my rails app? 在我的Rails控制器测试中,为什么会出现NoMethodError:字符串的未定义方法“格式”? - In my Rails controller test, why am I getting NoMethodError: undefined method 'formats' for string? 尝试在Rails 4应用程序中添加电子邮件确认时,为什么会出现未定义的方法错误? - Why am I getting an an undefined method error when I try to add email confirmations in my Rails 4 app? 为什么在AppFog上的Rails应用程序中出现NoMethodError? - Why am I getting NoMethodError with my rails app on AppFog? 为什么我的 Rails hash 键记法出现问题? - Why am I getting problems with my rails hash key notation? 使用Passenger 2.2.5,为什么我在Ruby on Rails页面上遇到以下错误,该错误是有关未引用的“ /”的? - Using Passenger 2.2.5, why am I getting the following error on my Ruby on Rails page about an unreferenced “/”? 为什么我的Rails应用程序中出现未定义的方法“ comments”? - Why am I getting undefined method `comments' in my rails app? 为什么我在rails中的has_one关系出现“未定义的方法”错误? - Why am I getting an “undefined method” error with my has_one relationship in rails? 尝试在Rails表单中添加占位符时,为什么会出现语法错误? - Why am I getting a Syntax Error when trying to add a placeholder to my Rails form? 为什么我的 Rails 上出现“Unpermitted parameters: :id, :reservation”错误? - Why am I getting an error on my rails for "Unpermitted parameters: :id, :reservation"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM