ruby on rails - Error Undefined method `admin?' for nil:NilClass - Seems simple won't work -


i have small problem ive tried fix hour. seems simple.

if goes user index view want them redirected root url. unless they're admin.

any clues how fix?

thank in advance.

i have simple test

  test "should redirect index when not logged in"     :index     assert_redirected_to login_url   end 

but receive error.

  1) error: userscontrollertest#test_should_redirect_index_when_not_logged_in: nomethoderror: undefined method `admin?' nil:nilclass     app/controllers/users_controller.rb:78:in `admin_user'     test/controllers/users_controller_test.rb:42:in `block in <class:userscontrollertest>' 

my admin_user method in user controller below

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

you need check if user has been logged in. if not, current_user nil. may want check in admin_user method too.

redirect_to(root_url) unless current_user.try(:admin?) 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -