这样做的目的是为了让管理员用户不会自毁。我写了以下测试:
describe "as admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before { valid_signin admin }
describe "should not be able to delete himself by submitting a DELETE request to the Users#destroy action" do
specify do
expect { delete user_path(admin) }.not_to change(User, :count).by(-1)
end
end
end并修改了销毁操作,如下所示:
def destroy
@user = User.find(params[:id])
unless current_user?(@user)
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
end(如果您是管理员用户,则只能访问销毁操作)。
测试现在应该通过了,但它没有通过。我得到以下错误消息:
Failure/Error: expect { delete user_path(admin) }.not_to change(User, :count).by(-1)
ActionView::MissingTemplate:
Missing template users/destroy, application/destroy with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}.我不理解缺少模板的错误消息,也不明白为什么测试不能通过。
发布于 2013-02-10 11:34:44
尝试将您的destroy操作更改为如下所示,看看您的测试是否通过:
def destroy
user = User.find(params[:id])
unless current_user?(user)
user.destroy
flash[:success] = "User destroyed."
else
flash[:error] = "You can't destroy yourself."
end
redirect_to users_url
end我认为问题在于,只有在成功销毁用户的情况下,您才会重定向到users_url。如果你不这样做(即管理员试图自毁),那么就没有重定向,Rails将开始查找名为destroy.html.erb,not find one anywhere的视图,并引发异常。这也是为什么方法中的用户变量从@user更改为user的原因:局部变量可以代替实例变量,因为它不需要在视图中使用。
如果这不是问题所在,请编辑您的问题,在当前代码中包含指向Github存储库的链接。
https://stackoverflow.com/questions/14794181
复制相似问题