0
votes

Working through Michael Hartl's tutorial, I got stuck for a while in Chapter 11.3 "Manipulating Microposts".

Here are the two errors that I got:

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:inadmin_user' test/controllers/users_controller_test.rb:48:in block (2 levels) in <class:UsersControllerTest>' test/controllers/users_controller_test.rb:47:inblock in ' app/controllers/users_controller.rb:64:in admin_user' test/controllers/users_controller_test.rb:48:inblock (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:inblock 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 '

After some research, I was able to get rid of the errors and get the tests to pass. However, even after all that work, I'm not sure why the change that I made caused the test to pass.

Here's what I did:

In the Users_Controller.rb I changed the 'before_action' for the logged_in_user from:

before_action :logged_in_user, only: [:edit, :update]

to:

before_action :logged_in_user, only: [:index, :edit, :update, :destroy]

That got rid of the admin? nil error. I'm still not sure why. Can anyone explain why the addition of the index and destroy caused the tests to pass

1

1 Answers

1
votes

I had the same problem.

Basically, it's because users have to be logged in to update their profile or delete a user. That's why :destroy and :update is added to the logged_in_user before filter. The logged_in_user method now being in ApplicationController from which the UserController inherits it. The MicropostControlle only needs :create and :destroy in that chapter.