non*_*ame 2 ruby ruby-on-rails actioncontroller ruby-on-rails-3 ruby-on-rails-4
我有一个用例问题.在Rails 4.1中,如果您运行一个控制器方法并且在方法中的某个点上具有redirect_to或render,那么您仍然可以在该点继续执行.如果您没有正确处理控制流,有时会导致AbstractController :: DoubleRenderError.为什么在Rails中允许这样做?这似乎是一个有趣的用例重定向而不是停止执行,何时这是合适的?
完整的错误消息如下所示:
AbstractController::DoubleRenderError:
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
Run Code Online (Sandbox Code Playgroud)
实际上错误信息说的都是:
请注意,您只能调用渲染或重定向,每次操作最多一次. 另请注意,重定向和呈现都不会终止操作的执行,因此如果要在重定向后退出操作,则需要执行类似"redirect_to(...)并返回"的操作.
这是默认的Rails行为.
如果您尝试在单个操作中呈现多个视图,则会收到AbstractController::DoubleRenderError错误.
要在渲染后停止执行,必须return显式使用语句.
| 归档时间: |
|
| 查看次数: |
2861 次 |
| 最近记录: |