2011-07-14 3 views
0

を実行しているアクションフォームを停止しませんfalseを返す前に、私はこれを実行します。RoRの:フィルターは私のテストで

 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) 

と私の前にフィルターを、プットを持っているので、私はdoesnの結果が偽である知っている、と」トンこのページによると

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 

ない、私は1つのテストのみを実行していること:

は、メソッドを破壊します。

+0

ポイントを、そしてより多くの*詳細を提供:

だから、あなたはおそらくしたいと思います。 –

+0

私は、行48をしました。 – DerNalia

答えて

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] 
+0

私のアプリがajaxをかなり重く使用し、リダイレクトの行にあまりにも多くのことをしないのはどうですか? Anywho、そこにredirect_toを追加しようとしましたが、それは未定義です。私はレールを使っています。2.3.8 – DerNalia

+0

@DerNalia - あなたがそのラムダの中にいるので、あなたは 'controller.redirect_to'を使う必要があるようです。 [head](http://rails.rubyonrails.org/classes/ActionController/Base.html#M000466)メソッドもあり、AJAXでうまくいく可能性があります。乾杯! –

+0

私はコントローラーをします。 NoMethodError:保護されたメソッドredirect_toが – DerNalia

関連する問題