控制器行动:
def deleteDept = {
def departmentInstance = Department.findByName(params.department.name)
if (!departmentInstance) {
println "no dept instance"
throw new org.codehaus.groovy.grails.exceptions.NewInstanceCreationException ("could not create DeptInstance for ${params.department.name}")
} else if (departmentInstance.paySvcs && !departmentInstance.paySvcs.isEmpty()){
println "instance with paySvcs"
// !!!! do not delete the department if it has payment services !!!!
departmentInstance.errors.reject('department.do.not.delete.message')
// render(view: "editDept", model: [departmentInstance: departmentInstance])
redirect(action: "editDept", id: departmentInstance.id)
} else{
println "proceed to delete"
try {
departmentInstance.delete(flush: true)
flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'department.label', default: 'Department'), departmentInstance.name])}"
redirect(action: …
Run Code Online (Sandbox Code Playgroud)