This message was deleted.
# questions
s
This message was deleted.
m
@Mohammed Faisal Try this, this code works for me, but I'm not clear about your front-end operation.
Copy code
class 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 }
        }
    }

}
m
@Michael Yan Thanks for your reply. Your suggested approach works, but I want to remove Books in the
def 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
Copy code
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
.
When I add a new row to the Books table in the UI and click on the Update button, the
def 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 ?
m
@Mohammed Faisal Your question is too big for me to reply to in detail here. I think what you need is a complete tutorial and real-world solution.