NoMethodError: .. .'admin?' while building Hartl RoR Sample_App

NoMethodError: .. .'admin?' while building Hartl RoR Sample_App

我快完成 ch 了。 Hartl 的 RoR 教程的第 9 篇,我的测试套件中不断出现三个错误。我用梳子梳了一遍,一切看起来都很好,但显然不是。

这是错误日志: https://gist.github.com/c7cf9884360c0b2bd05e.git

这是我的回购协议 https://github.com/kfrz/sample_app

砰!

fyi- 以后请在此处添加错误和相关代码,这样更容易(尽管在 github 上获得完整代码真的很棒!)。哦,对于那些来到这里的人来说,错误要点是

https://gist.github.com/kfrz/c7cf9884360c0b2bd05e

好的,所以在阅读了您的错误日志后,我们收到了 "admin?"

的未定义方法错误
ERROR["test_should_get_new", UsersControllerTest, 1.253923]
 test_should_get_new#UsersControllerTest (1.25s)
NoMethodError:         NoMethodError: undefined method `admin?' for nil:NilClass
            app/controllers/users_controller.rb:71:in `admin_user'
            test/controllers/users_controller_test.rb:17:in `block in <class:UsersControllerTest>'
        app/controllers/users_controller.rb:71:in `admin_user'
        test/controllers/users_controller_test.rb:17:in `block in <class:UsersControllerTest>'

这里的临界线是

app/controllers/users_controller.rb:71:in `admin_user'

看看控制器:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update]
  before_action :admin_user,  onlY: :destroy

#
#
#
#

   #confirm admin user
    def admin_user
      redirect_to(root_url) unless current_user.admin?
    end
end

问题其实在before_action

您在 before_action :admin_user, onlY: :destroy

中不小心将 "Y" 大写了

onlY: 更改为 only: 以使用前过滤器进行适当的访问控制(并且管理员只能执行销毁操作),您应该可以开始了!

最好的, TJ