0

我正在做 Michael Hartl 的 Rails 教程截屏视频 + 第 10 章的在线书籍,我被困在最后一个子章 Destroying User。

控制台输出:

Failures:

      1) UsersController DELETE 'destroy' as an admin user should destroy the user

         Failure/Error: delete :destroy, :id => @user
         NameError:
           undefined local variable or method `users' for #<UsersController:0x00000104a2f698>
          ./app/controllers/users_controller.rb:47:in `destroy'
          ./spec/controllers/users_controller_spec.rb:321:in `block (5 levels) in <top (required)>'
          ./spec/controllers/users_controller_spec.rb:320:in `block (4 levels) in <top (required)>'

      2) UsersController DELETE 'destroy' as an admin user should redirect to the users page

         Failure/Error: delete :destroy, :id => @user
         NameError:
           undefined local variable or method `users' for #<UsersController:0x000001049bf370>
         ./app/controllers/users_controller.rb:47:in `destroy'
         ./spec/controllers/users_controller_spec.rb:326:in `block (4 levels) in <top (required)>'

users_controller.rb

class UsersController < ApplicationController
  before_filter :authenticate, :only => [:index, :edit, :update, :destroy]
  before_filter :correct_user, :only => [:edit, :update]
  before_filter :admin_user, :only => :destroy

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

  def show
    @user = User.find(params[:id])
    @title = @user.name
  end

  def new
  @user = User.new
  @title = "Sign up"      
  end

  def create  
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      redirect_to @user, :flash => { :success => "Welcome to the Sample App!" }
    else
      @title = "Sign up"
      render 'new'
    end
  end

  def edit
    @title = "Edit user"
  end

  def update
    if @user.update_attributes(params[:user]) 
      redirect_to @user, :flash => { :success => "Profile updated." }
    else
    @title = "Edit user"
    render 'edit'
    end
  end

  def destroy
    User.find(params[:id]).destroy
    redirect_to users.path, :flash => { :success => "User destroyed." }
  end

  private

    def authenticate
      deny_access unless signed_in?
    end

    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end

    def admin_user
      @user = User.find(params[:id])
      redirect_to(root_path) if !current_user.admin? || current_user?(@user)
    end
end

users_controller_spec.rb:

    describe "DELETE 'destroy'" do

      before(:each) do
        @user = Factory(:user)
      end

      describe "as a non-signed-in user" do
        it "should deny access" do
          delete :destroy, :id => @user
          response.should redirect_to(signin_path)
        end
      end

      describe "as non-admin user" do
        it "should protect the action" do
          test_sign_in(@user)
          delete :destroy, :id => @user
          response.should redirect_to(root_path)
        end
      end

      describe "as an admin user" do

        before(:each) do
          @admin = Factory(:user, :email => "admin@example.com", :admin => true)
          test_sign_in(@admin)
        end

        it "should destroy the user" do
          lambda do
            delete :destroy, :id => @user
          end.should change(User, :count).by(-1)
        end

        it "should redirect to the users page" do
          delete :destroy, :id => @user
          flash[:success].should =~ /destroyed/i
          response.should redirect_to(users_path)
        end

        it "should not be able to destroy itself" do
          delete :destroy, :id => @admin
          lambda do
            delete :destroy, :id => @admin
          end.should_not change(User, :count)
        end
      end
  end   
end

我是初学者,但总是习惯于在此处发布任何问题之前进行广泛的研究(90% 的时间我通过搜索 SO 和 Google 找到答案)。

我最后 2 个未发布的问题(在发布前找到了答案)通过更新我的 Gemfiles(rspec 中的 will_paginate 和 undefined _selector)得到了修复。这也可以吗?

感谢您的关注并花时间阅读本文。

4

1 回答 1

5

找到了解决上述问题的错字(呃!):

def destroy
    User.find(params[:id]).destroy
    redirect_to users_path, :flash => { :success => "User destroyed." }
  end

而不是“users_path”我做了“users.path”

尝试从浏览器中删除时注意到它。

于 2011-10-22T03:09:56.770 回答