私は railstutorial.org で最新の Rails チュートリアルを行っていますが、特定の演習 ( http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec:updating_deleting_exercisesの #8) で行き詰まっています。 )。管理者が自分自身を削除できないようにするには、rspec/capybara テストを作成する必要があります。実装は機能していますが、テストを正しく機能させることができません。これが私のコードです。ここで同様の質問を見つけました: Ruby on Rails syntaxおよびhttps://getsatisfaction.com/railstutorial/topics/how_to_prevent_admin_user_from_deleting_themselves。しかし、それは古いチュートリアルであり、同じ質問ではないと思います。
spec/requests/user_pages_spec.rb の関連コードは次のとおりです。
describe "User pages" do
subject { page }
describe "delete links" do
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it "should not be able to delete themself" do
expect { admin.delete }.should_not change(User, :count)
end
end
end
end
end
エラー メッセージには、ユーザー数が 1 減っていることが示されています。
完全を期すために、ここに私の(作業中の)実装があります:
class UsersController < ApplicationController
before_filter :current_admin, only: :destroy
def current_admin
@user = User.find(params[:id])
redirect_to users_path, notice: "Cannot delete current admin" if current_user?(@user)
end
end
ありがとう、どこが間違っているの?(いくつかの方法を省略しましたが、うまくいけば、私がやろうとしていることを理解するのに十分です)
編集:Ruby v1.9.3、Rails v3.2.3を使用。デフォルトでは、管理者用の削除リンクはありません。
Edit2:これが私が取り組んだものです:
仕様/コントローラー/users_controller_spec.rb
require 'spec_helper'
describe UsersController do
describe "admins" do
let(:admin) { FactoryGirl.create(:admin) }
it "should not be able to delete themself" do
sign_in admin
expect { delete :destroy, :id => admin.id }.should_not change(User, :count)
end
end
end
users_controller.rb
def destroy
@user = User.find(params[:id])
if current_user?(@user)
flash[:error] = "Cannot delete current admin"
else
@user.destroy
flash[:success] = "User destroyed."
end
redirect_to users_path
end