简体   繁体   English

NoMethodError-未定义的方法nil:NilClass:

[英]NoMethodError - undefined method for nil:NilClass:

I am Rails noob and I am trying to unsderstand this simple JSON parsing code from a tutorial. 我是Rails noob,我想从教程中理解这个简单的JSON解析代码。 Why do I get the nil:NilClass Error? 为什么会出现nil:NilClass错误? What is a NilClass? 什么是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

Null , in Ruby is called nil and like everything else, nil is also an object. Null在Ruby中称为nil和其他所有东西一样, nil也是一个对象。 An object of NilClass . NilClass的对象。

You get this error if you try to call a method on a nil object. 如果尝试在nil对象上调用方法,则会收到此错误。

So in this case, requestUser is probably nil 因此,在这种情况下, requestUser可能为nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM