【问题标题】:NoMethodError (undefined method `user_id' for nil:NilClass):NoMethodError(nil:NilClass 的未定义方法“user_id”):
【发布时间】:2020-11-02 01:47:30
【问题描述】:

我创建了这个应用程序来显示节点的用户或所有者,但现在当我尝试登录时,应用程序失败并显示以下消息:

NoMethodError (undefined method `user_id' for nil:NilClass):

这是错误指向的代码行

users_controllers.rb

 def authenticate
    if params[:authentication] and authenticate = params[:authentication] and authenticate[:email].length > 0 and authenticate[:password].length > 0
      username = authenticate[:email]
      password = authenticate[:password]

      super_user = User.find(TheChosenOne.first.user_id) # This is the problematic line
      if username == super_user.email
        if Digest::SHA1.hexdigest(password) == TheChosenOne.first.password
          session[:user_id] = super_user.id
          redirect_to controller: :nodes, action: :list
        else
          redirect_to controller: :users, action: :login, alert: 'invalid_credentials' and return
        end
      else
        ret_result = nil
        command = Thread.new do
          ret_result  = `cd #{Rails.root}/Auth/src/; java AuthorizationService '#{username}' '#{password}'`
        end
        command.join

        if !ret_result.include? "true"
          redirect_to controller: :users, action: :login, alert: 'invalid_credentials' and return
        end

        user = User.find_by_email(username)
        if !User.exists?(user)
          user = User.create({email: username})
        end

我不确定是否需要在用户模型中创建 user_id 方法?这对我来说似乎没有多大意义。请我感谢我能得到的所有帮助。谢谢

【问题讨论】:

  • 您的会话变量未启动。请向我们展示您的整个代码
  • 你能验证TheChosenOne.first 是非空的吗?使用像pry 这样的调试器工具会有很长的路要走。
  • 与您最初的问题无关,但您的 shellout 存在未经身份验证的远程代码执行漏洞。通过使用whatever'; /path/to/evil; echo ' 之类的用户名,我可以在您的服务器上执行我想要的任何命令。此外,您不应使用未加盐的 SHA1 来存储您的密码。请改用has_secure_password
  • 我也可以使用任何随机用户名和密码(例如whatever'; echo 'true)完全跳过身份验证。您可能希望对所有参数使用Shellwords.shellescape,或者对您的外壳使用更安全的方法。
  • 正如@maxpleaner 建议的那样,TheChosenOne.first 返回 nil

标签: ruby-on-rails ruby sqlite


【解决方案1】:

很明显TheChosenOne.firstnil,而nil 没有#user_id 方法。

我假设您的问题基本上是:我如何找出 为什么 TheChosenOne.firstnil

我们将使用prybyebugrails console 来调试它。首先,确保 pry 已安装在您的 Gemfile 中(如果尚未安装):

group :development, :test do
  gem 'pry-byebug'       # pry integrated with byebug
  gem 'pry-rails'        # integrates pry with rails
end

bundle install,再启动rails console

% rails console
Running via Spring preloader in process 91323
Loading development environment (Rails 6.0.3.2)
[1] pry(main)> 

您应该能够执行TheChosenOne.first 并确认它是nil

[1] pry(main)> TheChosenOne.first
=> nil

pry 有一个很好的调试功能,可以检查带有$ 的方法的源代码:

[2] pry(main)> $ TheChosenOne.first

From: /myrails/app/models/the_chosen_one.rb:2:
Owner: #<Class:TheChosenOne>
Visibility: public
Signature: first()
Number of lines: 3

def self.first
  nil
end

我创建了一个虚拟示例,其中TheChosenOne.first 只返回nil,但您应该看到它是如何实现的以及它是在哪里定义的。使用该信息更深入地挖掘一个级别,以找出它返回 nil 的原因,因为它似乎不应该返回。

【讨论】:

  • 谢谢你,我很感激你的贡献。该应用程序现已重新上线。有趣的是,我不必更改我认为导致应用程序失败的代码行!
猜你喜欢
  • 2013-11-14
  • 2013-01-14
  • 2017-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多