Я делаю скринкаст Майкла Хартла по Rails Tutorial + онлайн-книгу по главе 10 и застрял в самой последней части главы «Уничтожение пользователя».
Вывод на консоль:
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 (will_paginate и undefined _selector в rspec).Может ли это быть тоже?
Спасибо за беспокойство и нашли время, чтобы прочитать это.