我在控制器中定义的前过滤器:如何防止在Rails中redirect_to之后的调用者继续?
before_filter :find, :only => [:caller]
,我要赶在“发现”的方法例外:
def find
begin
...
rescue Exception
redirect_to somewhere
end
end
,但我怎样才能防止“来电显示”方法持续执行?
我在控制器中定义的前过滤器:如何防止在Rails中redirect_to之后的调用者继续?
before_filter :find, :only => [:caller]
,我要赶在“发现”的方法例外:
def find
begin
...
rescue Exception
redirect_to somewhere
end
end
,但我怎样才能防止“来电显示”方法持续执行?
在救援中重定向后,您是否尝试过return
?
redirect_to ...
return # <<
end
end
如果before_filter
呈现或重定向,执行自动停止。
了解更多:http://guides.rubyonrails.org/action_controller_overview.html#filters
对于ActiveRecord
回调像before_validation
,使用return false
从被保存停止记录。
非常感谢! 这非常有帮助! – user233585 2009-12-18 06:28:12
我通常不喜欢使用的“和”(我更喜欢使用&&,以避免与优先级的问题),但我觉得它有用与重定向使用它: redirect_to的root_url并返回 它避免双渲染/重定向错误,并且对读取你的代码的其他人有意义。 – 2009-12-17 13:01:11