" NoMethodError:未定义的方法`admin?'为零:NilClass" - 测试错误

时间:2015-11-25 11:09:43

标签: ruby-on-rails

我目前正在通过Michael Hartl的Rails教程(第12章)学习Ruby on Rails。我突然得到以下错误。

的UsersControllerTest#test_should_redirect_destroy_when_not_logged_in: NoMethodError:未定义的方法admin?' for nil:NilClass app/controllers/users_controller.rb:92:in admin_user&#39;     test / controllers / users_controller_test.rb:48:在block (2 levels) in <class:UsersControllerTest>' test/controllers/users_controller_test.rb:47:in块中&#39;

这是我的测试代码:

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

我的其余代码:

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

有人可以看看,告诉我哪里出错了吗?代码基本上是从教程中复制的,所以我不知所措。

感谢。

3 个答案:

答案 0 :(得分:4)

我在Hartl教程中遇到了同样的错误。问题是遗漏:在users_controller.rb中列出9.53中的销毁操作。一旦发现缺少一点,测试就恢复了绿色。

那就是说,如果有人能够准确解释为什么会出现这种情况,那将会很棒。

正确的代码:

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

答案 1 :(得分:1)

像这样更改admin_user方法: -

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

这将首先检查current_user是否存在,然后检查第二个条件(current_user.admin?)。如果current_user.present?将是假的,然后它不会检查第二个条件。

答案 2 :(得分:0)

您需要更新admin_user方法以检查用户是否已登录。

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