How do I 'validate' on destroy in rails

前端 未结 11 1901
你的背包
你的背包 2020-11-28 06:33

On destruction of a restful resource, I want to guarantee a few things before I allow a destroy operation to continue? Basically, I want the ability to stop the destroy oper

11条回答
  •  长情又很酷
    2020-11-28 07:17

    It is what I did with Rails 5:

    before_destroy do
      cannot_delete_with_qrcodes
      throw(:abort) if errors.present?
    end
    
    def cannot_delete_with_qrcodes
      errors.add(:base, 'Cannot delete shop with qrcodes') if qrcodes.any?
    end
    

提交回复
热议问题