Wenn ich eine neue ID über das Webformular hinzufügen, bekomme ich das Wetter für eine neue Stadt. Aber wenn ID falsch ist, erhalte ich:undefined Methode für Nil: NilClass
„nicht definierte Methode` jeder ‚für nil: NilClass“ ...
Sagen Sie mir bitte, wie die Antwort des Servers zu überprüfen und die Meldung "Haben Sie die falsche ID eingegeben?"
Zum Beispiel als Reaktion auf die falsche ID:
{"cod":"404","message":"City not found: 605856"}
my_form:
<%= form_tag("search", method: "get") do %>
<%= label_tag(:q, "Search for:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("Go") %>
<% end %>
Controller:
@array = [703448,6058560,1819729] # ID cities
if params[:q].nil?
@cities = @array.join(",")
else
@array = @array << params[:q]
@cities = @array.join(",")
end
@lookup = Weather.call(@cities)
Modell:
class Weather
include HTTParty
base_uri "http://api.openweathermap.org/data/2.5/group?appid=***********************
format :json
#call the api with HTTParty and parse the JSON response
def self.call list_ids
response = HTTParty.get(base_uri + '&id=' + list_ids)
body = JSON.parse(response.body)
list = body["list"]
end
auf welcher Zeile erhalten Sie den Fehler? irgendwelche Rückverfolgung? –
list.each do | l | http://pix.academ.info/img/2017/07/12/5fe682a4ea2afa56e21fe6b66f5fc14d.png – Boris
Die 'Liste' ist null. Was ist die Antwort? Probieren Sie es aus und zeigen Sie uns das Ergebnis. – Pavan