3
votes

I am currently in the process of learning Ruby on Rails through Michael Hartl's Rails Tutorial (Chapter 12). I am suddenly getting the below error.

UsersControllerTest#test_should_redirect_destroy_when_not_logged_in: NoMethodError: undefined method admin?' for nil:NilClass app/controllers/users_controller.rb:92: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 '

Here is my test code:

test "should redirect destroy when not logged in" do
assert_no_difference 'User.count' do
  delete :destroy, id: @user
end
assert_redirected_to login_url
end

And the rest of my code:

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

def destroy
User.find(params[:id]).destroy
flash[:success] = "User deleted"
redirect_to users_url
end

def index
@users = User.paginate(page: params[:page])
end

def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
end

def new
@user = User.new
end

def create
@user = User.new(user_params)
if @user.save
  @user.send_activation_email
  flash[:info] = "Please check your email to activate your account."
  redirect_to root_url
else
  render 'new'
end
end

def edit
@user = User.find(params[:id])
end

def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
  flash[:success] = "Profile updated"
  redirect_to @user
else
  render 'edit'
end
end

def following
@title = "Following"
@user  = User.find(params[:id])
@users = @user.following.paginate(page: params[:page])
render 'show_follow'
end

def followers
@title = "Followers"
@user  = User.find(params[:id])
@users = @user.followers.paginate(page: params[:page])
render 'show_follow'
end


private

def user_params
  params.require(:user).permit(:name, :email, :password,
                               :password_confirmation)
end

# Before filters

# Confirms a logged-in user.
def logged_in_user
  unless logged_in?
  store_location
    flash[:danger] = "Please log in."
    redirect_to login_url
  end
end

# Confirms the correct user.
def correct_user
  @user = User.find(params[:id])
  redirect_to(root_url) unless current_user?(@user)
end

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

Can someone please have a look and tell me where I am going wrong? The code has basically been copied from the tutorial so I'm at a loss.

Thanks.

3
Your code cannot find current_user. Where you have defined your current_user for test cases? Are you using devise gem?RAJ

3 Answers

5
votes

I had this same error in the Hartl tutorial. The problem was a missed :destroy action in listing 9.53 in users_controller.rb. Tests were back to green once I found that missing bit.

That said, it would be great if someone could explain precisely why this is the case.

The correct code:

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

Change admin_user method like this:-

def admin_user
    redirect_to(root_url) unless current_user.present? && current_user.admin?
end

This will check first that current_user is present or not then check second condition(current_user.admin?). If current_user.present? will false then it will not check second condition.

0
votes

You need to update your admin_user method to check if user is even logged in.

# Confirms an admin user.
def admin_user
  redirect_to(root_url) if current_user.nil? || !current_user.admin?
end