在我的测试中,我这样做:
delete :destroy, :id => p.id
assert_equal "You do not have permission to delete this Object.", flash[:error]
Object.find_by_id(p.id).should_not be_nil #line 48 (reffed by console output below)
和我之前的过滤器,有一个put,所以我知道结果是false,并且不返回
before_filter lambda { |controller|
result = controller.is_object_on_same_account_as_current_account_for_id?(
controller_name.classify.constantize,
controller.params[:id])
puts result
return if result
}, :only => [:destroy]
根据此页面:http://citizen428.net/archives/846,过滤器绑定的操作仅在过滤器返回时执行。
这是控制台输出:
Started
false
F
Finished in 0.865708 seconds.
1) Failure:
test: .... ...
blah blah, issue is on line 48
shoulda (2.11.3) lib/shoulda/context.rb:382:in `...']:
expected: not nil
got: nil
那么......这里发生了什么?我完全无能为力。
相关代码:
破坏方法:
def destroy
@content.destroy
不,我只运行一次测试。
答案 0 :(得分:4)
根据官方Rails Docs,使用before_filter
停止执行操作的正确方法是在过滤器中实际重定向或呈现。
所以你可能想要:
before_filter lambda { |controller|
result = controller.is_object_on_same_account_as_current_account_for_id?(
controller_name.classify.constantize,
controller.params[:id])
puts result
if !result
flash[:error] = "You do not have permission to delete this Object."
redirect_to "/" # or wherever you want to redirect to
end
}, :only => [:destroy]