before_filter不取消动作 [英] before_filter not cancelling action

查看:139
本文介绍了before_filter不取消动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将过滤器在最近从1.9(?)升级到2.3.11的Rails应用程序中使用之前,我遇到了麻烦。为了对其进行调试,我在控制器中放置了一个before_filter:

I'm having trouble getting before filters to work in a Rails app I have recently upgraded from 1.9(?) to 2.3.11. To try and debug it, I have put a before_filter in a controller:

before_filter :false_filter

以及application_controller.rb中的以下内容:

and the following in application_controller.rb:

def false_filter
  puts "false filter running"
  false
end

然后我从Cucumber / Webrat或浏览器中调用该方法,并且在调用过滤器时(我可以看到puts输出消息),过滤器链不会终止。

I then call the method from either cucumber/webrat or a browser, and while the filter is getting called (I can see the puts outputting the message), the filter chain isn't getting terminated.

我想知道是否还没有生成一些样板代码。谁能建议在哪里看?

I'm wondering if there's some boilerplate code that hasn't been generated. Can anyone suggest where to look?

推荐答案

没有什么要注意前置过滤器的返回值。如果要停止处理,则必须从过滤器中渲染某些内容,或重定向到其他位置,精指南

Nothing pays any attention to a before-filter's return value. If you want to stop processing, you have to render something from your filter or redirect to somewhere else, from the fine guide:


如果前过滤器呈现或重定向,则该操作将不会运行。如果计划在该过滤器之后运行其他过滤器,则这些过滤器也将被取消。

If a before filter renders or redirects, the action will not run. If there are additional filters scheduled to run after that filter they are also cancelled.

5.2.0指南

如果过滤器链未完成(即部分停止过滤),则此行为确实有意义,那么您最终会以未按其期望的方式设置东西的情况下调用控制器方法,这只会造成痛苦,痛苦和困惑,那根本就不会友好或有趣。

This behavior does make sense, if the filter chain doesn't complete (i.e. stops filtering part way through) then you'd end up calling controller methods with things not set up the way they were expecting them to be and that would just cause pain, suffering, and confusion and that wouldn't be at all friendly or fun.

这篇关于before_filter不取消动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆