mardi 14 juillet 2015

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

I have a small problem that ive tried to fix for about an hour. It seems so simple.

If someone goes to the user index view i want them redirected to root url. Unless they're admin.

Any clues how to fix?

Thank you for any help in advance.

I have a simple test

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

But i receive this error.

  1) Error:
UsersControllerTest#test_should_redirect_index_when_not_logged_in:
NoMethodError: undefined method `admin?' for 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 is below

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

Aucun commentaire:

Enregistrer un commentaire