停止 AASM 过渡的最佳方法是什么
当成功调用的方法或进入状态转换阶段抛出错误时,捕获此错误并确保状态恢复到先前状态的最佳方法是什么。
我正在使用 AASM 宝石。
When a method being called in the success or enter phases of a state transition throw errors, what is the best way to catch this and ensure that the state reverts back to the previous state.
I'm using the AASM gem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如何使用 :guard 选项来确保可以执行转换? 如果防护方法返回 false,则根本不会执行转换。 因此,我首先检查守卫方法是否满足转换的每个条件。
What about using the :guard option to make sure the transition can be performed? If the guard method returns false, the transition isn't executed at all. So, I'd check if every condition of the transition is met with the guard method, first.
我没有使用过这个插件,但阅读了代码,除非我弄错了,否则似乎没有任何机制来传达这些回调失败的情况。
对于我来说,这对于
success
回调来说是完全有意义的,因为只有在转换实际发生时才会调用它,到那时就已经来不及恢复了。 如果您希望在success
回调中引发异常以阻止转换,也许代码应该放入转换本身而不是success
回调中......? 或者也许你需要一个额外的状态......?关于
enter
回调,guard
回调不是更合适吗...? 如果转换不应该继续,您可以返回 false。I've not used this plugin, but reading the code, unless I'm mistaken there doesn't appear to be any mechanism for communicating that either of these callbacks have failed.
This makes complete sense to me for the
success
callback, because it is only called once the transition has actually happened by which time it is too late to revert. If you want the exception raised within yoursuccess
callback to prevent the transition, perhaps the code should go in the transition itself and not in thesuccess
callback...? Or maybe you need an extra state...?Regarding the
enter
callback, would theguard
callback not be more appropriate...? You can return false if the transition should not go ahead.