0

我是 Rails 新手,并且使用Trestle Admin作为我的应用程序的简单后端解决方案。

当我尝试删除栈桥管理后端中的项目时,出现以下错误:

PG::ForeignKeyViolation:错误:更新或删除表“AAA”违反了表“BBB”上的外部 > 键约束“fk_rails_xxxxxx” 详细信息:键 (id)=(2) 仍从表“BBB”中引用。

那很好,但我不想显示应用程序错误,而是想检查错误并显示自定义警报消息。我不知道这可能与支架有关。有人知道如何存档吗?

谢谢

4

1 回答 1

0

我发现我可以做这样的事情,这解决了我的问题:

controller do
 def index; end

 def destroy
   if SecondModal.where(xxx_id: params[:id]).length > 0
     flash[:error] = "Record was not destroyed, because it's still referenced in other tables"
     redirect_to admin.path(:index)
   else
     FirstModal.delete(params[:id])
     flash[:success] = 'Record destroyed'
     redirect_to admin.path(:index)
   end
 end
end
于 2022-02-18T10:26:58.203 回答