Ruby on rails RoR:在筛选器返回false之前,不会停止操作窗体的运行

Ruby on rails RoR:在筛选器返回false之前,不会停止操作窗体的运行,ruby-on-rails,Ruby On Rails,在我的测试中,我会这样做: 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) 我的before过滤器有一个puts,所以我知道

在我的测试中,我会这样做:

     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)
我的before过滤器有一个puts,所以我知道结果是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]
根据此页面:,只有当筛选器返回时,才会执行筛选器绑定的操作

以下是控制台输出:

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 
不是,我只是在做一个测试

根据官员的说法,在过滤器之前使用
阻止操作执行的正确方法是在过滤器内实际重定向或渲染

所以你可能想要:

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]
根据这位官员的说法,阻止在过滤器之前使用
执行操作的正确方法是在过滤器内实际重定向或渲染

所以你可能想要:

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]

指出失败的代码,并提供更多详细信息。指出失败的代码,并提供更多详细信息。如果我的应用程序大量使用ajax,并且在重定向方面没有做太多工作,该怎么办?不管是谁,我试着在那里加了一个重定向到。。。它说它是未定义的。我使用的是rails 2.3。8@DerNalia-看起来您需要使用
控制器。重定向到
,因为您在lambda中。还有一种方法,可以更好地使用AJAX。干杯当我做控制器时,我得到了以下信息:NoMethodError:protected方法'redirect_to'calledIt建议为每个筛选器使用单独的方法:
在\u筛选器之前:检查\u;def check_whatever…
目标是在我的所有控制器上使用一个简单的before_过滤器,我想在某些情况下阻止销毁方法。还有办法吗?因为我讨厌重复的代码。如果我的应用程序大量使用ajax,并且在重定向方面没有做太多的工作,该怎么办?不管是谁,我试着在那里加了一个重定向到。。。它说它是未定义的。我使用的是rails 2.3。8@DerNalia-看起来您需要使用
控制器。重定向到
,因为您在lambda中。还有一种方法,可以更好地使用AJAX。干杯当我做控制器时,我得到了以下信息:NoMethodError:protected方法'redirect_to'calledIt建议为每个筛选器使用单独的方法:
在\u筛选器之前:检查\u;def check_whatever…
目标是在我的所有控制器上使用一个简单的before_过滤器,我想在某些情况下阻止销毁方法。还有办法吗?因为我讨厌重复的代码。