ravenUSMC
ravenUSMC

Reputation: 517

undefined method `[]' for nil:NilClass Rails 4

I am working on a weather app using the Weather Underground API. Everything almost appears to be working fine in my web app. Except for one thing. When ever I start up my server and head to my index page, in this case, the main page. I get the following error message: undefined method []' for nil:NilClass. Checking my logs, I see the following message: NoMethodError (undefined method '[]' for nil:NilClass):app/controllers/welcome_controller.rb:14 in index.

now my controller looks like this:

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

Now, I have tracked down the problem, I believe, to the params :state and :city not being filled in when I load the page. If I delete this part of the code:

  @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']

Then load the page, everything will work fine if I select a state and city, then add the above deleted code-it will pull it up. Except I cannot start my server and go directly to my index page or else it will crash. I also tried placing the following in:

params[:state] = "MA"
params[:city] = "Boston"

and that will load the page just fine except I am stuck on Boston! Finally, Here are my routes:

  #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'

Any help will be great! I also have all of my code posted at github, username is ravenusmc. Again, thank you for the help.

Upvotes: 1

Views: 1426

Answers (1)

Leo Brito
Leo Brito

Reputation: 2051

One or more fields of response are probably nil. This is a very common mistake; you should always check if the variable is nil or empty before trying to access nested hash keys or array positions. e.g., insted of @location = response['location']['city'], use something like:

@location = response['location'] ? response['location']['city'] : nil

Do the same for the rest of the @location = response... attributions.

If you're using ruby 2.3.0, you can use the dig method:

@location = response.dig('location', 'city') 

Which handles nil values for you. See the difference:

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>'

Upvotes: 3

Related Questions