[英]undefined method `[]' for nil:NilClass Rails 4
我正在使用Weather Underground API處理天氣應用程序。 在我的網絡應用程序中,一切似乎都運行正常。 除了一件事。 什么時候我啟動我的服務器並前往我的索引頁面,在這種情況下,主頁面。 我收到以下錯誤消息:未定義的方法[]' for nil:NilClass. Checking my logs, I see the following message: NoMethodError (undefined method '[]' for nil:NilClass):app/controllers/welcome_controller.rb:14
[]' for nil:NilClass. Checking my logs, I see the following message: NoMethodError (undefined method '[]' for nil:NilClass):app/controllers/welcome_controller.rb:14
index
[]' for nil:NilClass. Checking my logs, I see the following message: NoMethodError (undefined method '[]' for nil:NilClass):app/controllers/welcome_controller.rb:14
。
現在我的控制器看起來像這樣:
class WelcomeController < ApplicationController
def index
#states will need to be defined and then @states.sort will sort all of them on the form.
@states = %w(HI AK CA OR WA ID UT NV AZ NM CO WY MT ND SD NE KS OK TX LA AR
MO IA MN WI IL IN MI OH KY TN MS AL GA FL SC NC VA WV DE MD PA NY NJ CT RI
MA VT NH ME DC PR)
@states.sort!
#Here is the call to the API
response = HTTParty.get("http://api.wunderground.com/api/#
{ENV['wunderground_api_key']}/geolookup/conditions/q/#{params[:state]}/#
{params[:city]}.json")
@location = response['location']['city']
@temp_f = response['current_observation']['temp_f']
@temp_c = response['current_observation']['temp_c']
@weather_icon = response['current_observation']['icon_url']
@weather_words = response['current_observation']['weather']
@forecast_link = response['current_observation']['forecast_url']
@real_feel = response['current_observation']['feelslike_f']
#This part of the code will change the background depending on what
@weather_words is.
#Head over to the views/layouts/application.html.erb file to see more.
if @weather_words == "Partly Cloudy" || @weather_words == "Mostly Cloudy"
@body_class = "partly-cloudy"
elsif @weather_words == "Cloudy" || @weather_words == "Scattered Clouds" || @weather_words == "Overcast"
@body_class = "partly-cloudy"
elsif @weather_words == "Clear"
@body_class = "sunny"
elsif @weather_words == "snow"
@body_class = "snow"
elsif @weather_words == "Rain"
@body_class = "rain"
elsif @weather_words == "Fog"
@body_class = "fog"
elsif @weather_words == "Thunderstorms and Rain" || @weather_words == "Thunderstorms"
@body_class = "thunder"
end
end
現在,我已經找到了問題,我相信,對於params:state和:在我加載頁面時沒有填寫城市。 如果我刪除這部分代碼:
@location = response['location']['city']
@temp_f = response['current_observation']['temp_f']
@temp_c = response['current_observation']['temp_c']
@weather_icon = response['current_observation']['icon_url']
@weather_words = response['current_observation']['weather']
@forecast_link = response['current_observation']['forecast_url']
@real_feel = response['current_observation']['feelslike_f']
然后加載頁面,如果我選擇一個州和城市,一切都會正常工作,然后添加上面刪除的代碼 - 它會將其拉出來。 除了我無法啟動我的服務器並直接進入我的索引頁面,否則它將崩潰。 我還嘗試將以下內容放入:
params[:state] = "MA"
params[:city] = "Boston"
這將加載頁面就好了,除了我被困在波士頓! 最后,這是我的路線:
#The index page gets two routes:
#The get route for when we initially come to the page
get 'index' => 'welcome#index'
#And then a post route for when we come back to the index page after
# submitting the form
post 'index' => 'welcome#index'
任何幫助都會很棒! 我的所有代碼都發布在github上,用戶名是ravenusmc
。 再次,謝謝你的幫助。
一個或多個response
字段可能為零。 這是一個非常常見的錯誤; 在嘗試訪問嵌套的哈希鍵或數組位置之前,應始終檢查變量是否為空或空。 例如, @location = response['location']['city']
,請使用以下內容:
@location = response['location'] ? response['location']['city'] : nil
對@location = response...
attributions的其余部分執行相同的@location = response...
。
如果您使用的是ruby 2.3.0,則可以使用dig
方法:
@location = response.dig('location', 'city')
它為您處理零值。 看到不同:
2.3.0 :004 > response = {}
# => {}
2.3.0 :005 > response.dig('location', 'city')
# => nil
2.3.0 :006 > response['location']['city']
NoMethodError: undefined method `[]' for nil:NilClass
from (irb):6
from /home/lbrito/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.