为什么更改 before_action 会消除此 "undefined method 'admin' " 错误(第 11 章 Hartl 教程)
Why does changing the before_action get rid of this "undefined method 'admin' " error (Chapter 11 Hartl tutorial)
通过 Michael Hartl 的教程,我在第 11.3 章中卡住了一段时间 "Manipulating Microposts"。
这是我遇到的两个错误:
ERROR["test_should_redirect_destroy_when_not_logged_in", UsersControllerTest, 2016-01-21 11:50:23 +0000]
test_should_redirect_destroy_when_not_logged_in#UsersControllerTest (1453377023.37s)
NoMethodError: NoMethodError: undefined method admin?' for nil:NilClass
app/controllers/users_controller.rb:64:in
admin_user'
test/controllers/users_controller_test.rb:48:in block (2 levels) in <class:UsersControllerTest>'
test/controllers/users_controller_test.rb:47:in
block in '
app/controllers/users_controller.rb:64:in admin_user'
test/controllers/users_controller_test.rb:48:in
block (2 levels) in '
test/controllers/users_controller_test.rb:47:in `block in '
ERROR["test_should_redirect_index_when_not_logged_in", UsersControllerTest, 2016-01-21 11:50:23 +0000]
test_should_redirect_index_when_not_logged_in#UsersControllerTest (1453377023.81s)
ActionView::Template::Error: ActionView::Template::Error: undefined method admin?' for nil:NilClass
app/views/users/_user.html.erb:4:in
_app_views_users__user_html_erb___1165587237033555937_81443380'
app/views/users/index.html.erb:7:in _app_views_users_index_html_erb___836252608784755247_81359900'
test/controllers/users_controller_test.rb:11:in
block in '
app/views/users/_user.html.erb:4:in _app_views_users__user_html_erb___1165587237033555937_81443380'
app/views/users/index.html.erb:7:in
_app_views_users_index_html_erb___836252608784755247_81359900'
test/controllers/users_controller_test.rb:11:in `block in '
经过一些研究,我能够消除错误并通过测试。但是,即使完成所有这些工作,我也不确定为什么我所做的更改会导致测试通过。
这是我所做的:
在 Users_Controller.rb 中,我将 logged_in_user 的 'before_action' 更改为:
before_action :logged_in_user, only: [:edit, :update]
至:
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
那摆脱了管理员?零错误。我仍然不确定为什么。谁能解释为什么添加索引和销毁导致测试通过
我遇到了同样的问题
基本上,这是因为用户必须登录才能更新他们的个人资料或删除用户。这就是为什么 :destroy 和 :update 被添加到 logged_in_user before filter 的原因。 logged_in_user 方法现在在 ApplicationController 中,UserController 从中继承它。 MicropostControlle 只需要在那一章中的 :create 和 :destroy。
通过 Michael Hartl 的教程,我在第 11.3 章中卡住了一段时间 "Manipulating Microposts"。
这是我遇到的两个错误:
ERROR["test_should_redirect_destroy_when_not_logged_in", UsersControllerTest, 2016-01-21 11:50:23 +0000] test_should_redirect_destroy_when_not_logged_in#UsersControllerTest (1453377023.37s) NoMethodError: NoMethodError: undefined method
admin?' for nil:NilClass app/controllers/users_controller.rb:64:in
admin_user' test/controllers/users_controller_test.rb:48:inblock (2 levels) in <class:UsersControllerTest>' test/controllers/users_controller_test.rb:47:in
block in ' app/controllers/users_controller.rb:64:inadmin_user' test/controllers/users_controller_test.rb:48:in
block (2 levels) in ' test/controllers/users_controller_test.rb:47:in `block in 'ERROR["test_should_redirect_index_when_not_logged_in", UsersControllerTest, 2016-01-21 11:50:23 +0000] test_should_redirect_index_when_not_logged_in#UsersControllerTest (1453377023.81s) ActionView::Template::Error: ActionView::Template::Error: undefined method
admin?' for nil:NilClass app/views/users/_user.html.erb:4:in
_app_views_users__user_html_erb___1165587237033555937_81443380' app/views/users/index.html.erb:7:in_app_views_users_index_html_erb___836252608784755247_81359900' test/controllers/users_controller_test.rb:11:in
block in ' app/views/users/_user.html.erb:4:in_app_views_users__user_html_erb___1165587237033555937_81443380' app/views/users/index.html.erb:7:in
_app_views_users_index_html_erb___836252608784755247_81359900' test/controllers/users_controller_test.rb:11:in `block in '
经过一些研究,我能够消除错误并通过测试。但是,即使完成所有这些工作,我也不确定为什么我所做的更改会导致测试通过。
这是我所做的:
在 Users_Controller.rb 中,我将 logged_in_user 的 'before_action' 更改为:
before_action :logged_in_user, only: [:edit, :update]
至:
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
那摆脱了管理员?零错误。我仍然不确定为什么。谁能解释为什么添加索引和销毁导致测试通过
我遇到了同样的问题
基本上,这是因为用户必须登录才能更新他们的个人资料或删除用户。这就是为什么 :destroy 和 :update 被添加到 logged_in_user before filter 的原因。 logged_in_user 方法现在在 ApplicationController 中,UserController 从中继承它。 MicropostControlle 只需要在那一章中的 :create 和 :destroy。