2011-07-14 104 views
0

所以,我一直在試圖設置一個before_filter來檢查是否有人可以刪除一個對象的權限。但是,一直沒有工作......最終我做到以下幾點:Ruby on Rails:2.3.8,過濾器自定義之前不工作?

before_filter :test_hack, :only => :destroy 

    def test_hack 
    return false 
    end 

destroy方法在這裏:

def destroy 
    @content = Content.find(params[:id]) 

#will get rid of this when the before filter works... 
# but this doesn't stop it from getting deleted either 
    if not has_permission_to_change?(@content) 
     puts "This content is not gonig to get deleted" 
     flash[:error] = 'You do not have permission to delete this content.' 
    else 
     @content.destroy 
    end 

失敗的測試:

should "not allow the deleting of #{plural_name} on different accounts" do 
     login_as(@user) 
     p = Factory(factory_name, :account => Factory(:account)) 

     assert_difference("#{klass}.count", 0) do 
     begin 
      delete :destroy, :id => p.id 
      raise "program flow should not reach this message" 
     rescue ActiveRecord::RecordNotFound 
      assert true 
     end 
     end 

內容belongs_to的賬戶

控制檯輸出:

Loaded suite test/functional/contents_controller_test 
Started 
This content is not gonig to get deleted 
E 
Finished in 0.649422 seconds. 

    1) Error: 
test: destroy contents! should not allow the deleting of contents on different accounts. (ContentsControllerTest): 
RuntimeError: program flow should not reach this message 
+0

有人回答你的其他問題:http://stackoverflow.com/questions/6683113/ruby-on-rails-2-3-8-freaking-destroy-behavior-ignores-before-filter – ryeguy

+0

@ryeguy:謝謝,我剛剛;) – apneadiving

回答

1

Once again,測試的bahavior完全是正常現象:

你行raise "program flow should not reach this message"就一定會執行,因爲存在與物體id傳遞:您剛纔創建它

你應該保持:

assert_difference("#{klass}.count", 0) do 
    delete :destroy, :id => p.id 
    end 

我an't看到你before_filter在這裏有用

1

在您的測試,

delete :destroy, :id => p.id 

不會產生任何異常,所以執行繼續正常,在到達下一行

raise "program flow should not reach this message" 

和測試失敗,因爲這不是抓。 before_filter與它無關,根據測試輸出,它甚至沒有被調用。

+0

同樣的答案,同一時間,相同的結論,+1 – apneadiving

相關問題