gpt4 book ai didi

ruby-on-rails - 抛出异常时,before_remove 中止删除会使服务器崩溃

转载 作者:行者123 更新时间:2023-12-04 06:22:59 26 4
gpt4 key购买 nike

我正在使用 Rails 5 的关联回调。我在文档中读到,如果要中止从关联中删除对象,则必须抛出异常:

If a before_add callback throws an exception, the object does not get added to the collection. Similarly, if a before_remove callback throws an exception, the object does not get removed from the collection.



我正在这样做,但服务器崩溃了:
UncaughtThrowError (uncaught throw :abort)

这是我的代码的样子:
def can_remove_association(object)
return unless object.condition
errors[:base] << "Can't be removed."
throw(:abort)
end

有什么线索吗? throw(:abort)正在避免要移除的对象,但我得到了一个丑陋的 500!

一些引用: http://blog.bigbinary.com/2016/02/13/rails-5-does-not-halt-callback-chain-when-false-is-returned.html

最佳答案

关于ruby-on-rails - 抛出异常时,before_remove 中止删除会使服务器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41743011/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com