chipmunk
chipmunk

Reputation: 565

NoMethodError - undefined method for nil:NilClass:

I am Rails noob and I am trying to unsderstand this simple JSON parsing code from a tutorial. Why do I get the nil:NilClass Error? What is a NilClass?

Thanks!

    app.put '/users/update' do
    params = JSON.parse(request.body.read)

    reqUserID = params[:id]

    requestUser = Models::Persistence::User.find_by_id(reqUserID)

    content_type "application/json"
    puts "Hello"
    puts requestUser.username

    if (requestUser)
      status 401
      return
    end

Upvotes: 0

Views: 730

Answers (1)

Santhosh
Santhosh

Reputation: 29124

Null, in Ruby is called nil and like everything else, nil is also an object. An object of NilClass.

You get this error if you try to call a method on a nil object.

So in this case, requestUser is probably nil

Upvotes: 2

Related Questions