title: undefined method `[]' for nil:NilClass Rails 4
body: <p>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 <code>[]' for nil:NilClass. Checking my logs, I see the following message: NoMethodError (undefined method '[]' for nil:NilClass):app/controllers/welcome_controller.rb:14</code> in <code>index</code>. </p>
  
  <p>now my controller looks like this: </p>
  
  <pre><code>class WelcomeController &lt; 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
  </code></pre>
  
  <p>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: </p>
  
  <pre><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']
  </code></pre>
  
  <p>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: </p>
  
  <pre><code>params[:state] = "MA"
  params[:city] = "Boston"
  </code></pre>
  
  <p>and that will load the page just fine except I am stuck on Boston! Finally, Here are my routes: </p>
  
  <pre><code>  #The index page gets two routes:
    #The get route for when we initially come to the page
    get 'index' =&gt; 'welcome#index'
    #And then a post route for when we come back to the index page after     
    # submitting the form
    post 'index' =&gt; 'welcome#index'
  </code></pre>
  
  <p>Any help will be great! I also have all of my code posted at github, username is <code>ravenusmc</code>. Again, thank you for the help. </p>