Grails:动态形式

Grails:动态形式,grails,Grails,我对从书中删除作者有问题 我有一对多单向关系中的班级课本和班级作者: class Book{ String isbn ... List<Author> authors static hasMany = [ authors: Author ] } class Author { String firstname String lastname ... } 对于添加作者和编辑作者来说,它工作得很好,但对于删除作者来说,它不

我对从书中删除作者有问题

我有一对多单向关系中的班级课本和班级作者:

class Book{

    String isbn
    ...

    List<Author> authors
    static hasMany = [ authors: Author ]
}

class Author {

    String firstname
    String lastname
    ...

}
对于添加作者编辑作者来说,它工作得很好,但对于删除作者来说,它不起作用。 如果我使用“删除作者”按钮并单击“提交表单”,则作者不会从书本中删除。 所有的作者都和编辑前一样

拜托,你能帮我解决这个问题吗? 这是解决这个问题的好办法吗

非常感谢
Tom

Grails无法知道要删除哪个作者,除非您在请求中发送要删除的作者ID,并向Grails发送删除该作者的明确指令。到目前为止,params中没有作者并不意味着删除作者

因此,您需要在表单中执行一些操作,以便在“删除作者”时,将要删除的作者的id存储在隐藏字段中,以及指示要删除的内容。然后像


不需要获取author的完整对象,只需加载其代理也可以。def authorToRemove=Author.load(params.authord)您是如何在JS中生成表单的?
 def save = {
            def book = new Book()

            if (params.id){
                book = Book.get(params.id)                     
            }

            book.properties = params

            if( book.save() ){
                redirect(controller:"book", action:"all")
                return
            }else{
                render(view:'form', model:["book": book])
                return
            }
        }
def authorToRemove = Author.get(params.authorId)
def book = Book.get(params.id)
book.removeFromAuthors(authorToRemove)