undefined method `keys' for nil:NilClass for saas hw2

ruby, ruby-on-rails

Solution

Let's walk through that error message...

undefined method `keys' for nil:NilClass

There are three important pieces to this:

- `undefined method` - This is telling you the core problem. The problem is that the method you're trying to call does not exist for the thing that you're trying to call it on.

- `keys` - This is telling you the method that you're attempting to call.

- `nil:NilClass` - This is telling you what you're calling the method on. In your case, this bit of information isn't directly helpful - it's not telling you exactly what to look for. It is telling you, however, that whatever you're looking for has a value of `nil`.

`Rails.root: C:/Sites/RailsProjects/hw2_rottenpotatoes`

This is telling you the root of your project, just in case you've completely forgotten what you're even working on. It's ok. We all have those days.

Application Trace | Framework Trace | Full Trace
app/controllers/movies_controller.rb:24:in `block in index'
app/controllers/movies_controller.rb:23:in `each'
app/controllers/movies_controller.rb:23:in `index'

This is telling you exactly where to look for the error that you're hitting. It's right there in the second line: `app/controllers/movies_controller.rb:24` ... File `movies_controller.rb`, line `24`.

It's probably referring to this line:

if @ratings.keys.include? mv[:rating]

You're checking to see if `mv[:rating]` is in `@ratings.keys` ... But the error that you got is telling you that you're checking for `keys` on `nil`. That means that `@ratings` hasn't been set.

So, it looks like you'll just need to set `@ratings` somewhere near the top of that `index` action.

Problem

I am trying to run an app on Rails, for a saas course, homework 2. Whenever I refresh the page, I get the following error: ``` NoMethodError in MoviesController#index undefined method `keys' for nil:NilClass Rails.root: C:/Sites/RailsProjects/hw2_rottenpotatoes Application Trace | Framework Trace | Full Trace app/controllers/movies_controller.rb:24:in `block in index' app/controllers/movies_controller.rb:23:in `each' app/controllers/movies_controller.rb:23:in `index' ``` My `movies_controller.rb` file: ``` class MoviesController < ApplicationController def show id = params[:id] # retrieve movie ID from URI route @movie = Movie.find(id) # look up movie by unique ID end def index redirect = false if params[:sort] @sorting = params[:sort] elsif session[:sort] @sorting = session[:sort] redirect = true end if redirect redirect_to movies_path(:sort => @sorting, :ratings => @ratings) end Movie.find(:all, :order => @sorting ? @sorting : :id).each do |mv| if @ratings.keys.include? mv[:rating] (@movies ||= [ ]) << mv end end session[:sort] = @sorting session[:ratings] = @ratings end def new # default: render 'new' template end def create @movie = Movie.create!(params[:movie]) flash[:notice] = "#{@movie.title} was successfully created." redirect_to movies_path end def edit @movie = Movie.find params[:id] end def update @movie = Movie.find params[:id] @movie.update_attributes!(params[:movie]) flash[:notice] = "#{@movie.title} was successfully updated." redirect_to movie_path(@movie) end def destroy @movie = Movie.find(params[:id]) @movie.destroy flash[:notice] = "Movie '#{@movie.title}' deleted." redirect_to movies_path end end ``` I am very very new to Rails, and have been stuck on just this one point for like 4 hours trying different things.

Original source