Slackbot
02/13/2024, 6:53 AMMichael Yan
02/13/2024, 3:43 PMclass AuthorController {
def removeBook(Long authorId, Long bookId) {
def author = authorService.get(bookId)
def bookToRemove = bookService.get(bookId)
if (bookToRemove) {
author.removeFromBooks(bookToRemove)
authorService.save(author)
}
request.withFormat {
'*'{ render status: NO_CONTENT }
}
}
}Mohammed Faisal
02/15/2024, 6:17 AMdef update(Author author) method of AuthorController generated by the dynamic scaffolding, upon data binding the request parameters - as it correctly does for creating and updating Books. This is the method
def update(Author author) {
if (author == null) {
notFound()
return
}
try {
authorService.save(author)
} catch (ValidationException e) {
respond author.errors, view:'edit'
return
}
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.updated.message', args: [message(code: 'author.label', default: 'Author'), author.id])
redirect author
}
'*'{ respond author, [status: OK] }
}
}
I have attached a screenshot of the UI, where
• Add Book button will add a new row to the books table - a simple DOM operation only (book is not added to the database at this point)
• Remove Book button will remove a row from the books table - a simple DOM operation only (book is not removed from the database at this point)
• Update button will submit the author fields and list of books to the server and this request is handling by the above mentioned def update(Author author) of AuthorController.Mohammed Faisal
02/15/2024, 6:32 AMdef update(Author author) method of AuthorController will correctly bind the request parameters and then create and add that Book to the hasMany association of Author.
My question is about how should I send data to the update method from UI upon clicking the Update button, so that Grails will remove Books from the hasMany list of Author ?Mohammed Faisal
02/15/2024, 7:36 AMMichael Yan
02/20/2024, 4:13 PM