Ruby-on-rails before_filter 不取消操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6353286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:24:30  来源:igfitidea点击:

before_filter not cancelling action

ruby-on-railsbefore-filter

提问by Henry Collingridge

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:

我在最近从 1.9(?) 升级到 2.3.11 的 Rails 应用程序中使用过滤器之前遇到问题。为了尝试调试它,我在控制器中放置了一个 before_filter:

before_filter :false_filter

and the following in application_controller.rb:

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

def false_filter
  puts "false filter running"
  false
end

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.

然后我从黄瓜/webrat 或浏览器调用该方法,当过滤器被调用时(我可以看到 puts 输出消息),过滤器链没有被终止。

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

我想知道是否有一些未生成的样板代码。任何人都可以建议在哪里看?

回答by mu is too short

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:

没有任何注意过滤器之前的返回值。如果你想停止处理,你必须从你的过滤器中渲染一些东西或重定向到其他地方,从 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.

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

The same text appears in the 5.2.0 guide.

相同的文本出现在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.

这种行为是有道理的,如果过滤器链没有完成(即停止过滤部分通过),那么你最终会调用控制器方法,但事情没有按照他们期望的方式设置,这只会引起痛苦、痛苦和困惑,这一点都不友好或有趣。