我有两个域类 Author 和 Book 相关的一对多。当我为特定作者创建一本新书时,我想(在单击保存按钮后)重定向到作者 Controller 的显示操作,以便显示特定作者的全部详细信息以及他创作的所有书籍的列表。
class Author {
String fistName
String secondName
static hasMany = [books: Book]
}
class Book {
String title
String dateOfPublication
//Author author
static belongsTo = [author: Author]
}
//BookController
def save() {
def bookInstance = new Book(params)
if (!bookInstance.save(flush: true)) {
render(view: "create", model: [bookInstance: bookInstance])
return
}
flash.message = message(code: 'default.created.message', args: [
message(code: 'book.label', default: 'Book'),
bookInstance.id])
if(params.tempid){
redirect(action: "show", controller: "author", id: params.id)
}else{
redirect(action: "show", id: params.tempid)
}
}
然而,上面的代码并没有按照我的意愿重定向我,而是执行了 else 语句,并显示了 flash 消息“未找到 ID 为 null 的书”
下面是在 Author 类的 show.gsp 中呈现的 _book.gsp 模板。
<table class="table table-striped table-hover" id="book-data">
<thead>
<th class="sortable">Title</th>
<th class="sortable">Date of Publication</th>
</thead>
<tbody>
<g:each in="${authorInstance.book}" status="i" var="p">
<tr class="prop">
<td style="vertical-align: middle;" valign="top" class="value">${p?.title}</td>
<td style="vertical-align: middle;" valign="top" class="value">${p?.dateOfPublication}</td>
<td style="vertical-align: middle;" valign="top"><g:link class="btn btn-edit" action="edit" controller="book" id="${p?.id}"> Edit</g:link></td>
</tr>
</g:each>
</tbody>
</table>
我怎样才能做到这一点。?
感谢帮助。
编辑:authorInstance.id 更改为 params.id
最佳答案
如果您想在保存 book
后重定向到您的作者实例,你应该发送作者的id
在保存请求参数中。默认情况下,作者 ID 包含在 params
中映射为 author.id
.
所以这应该工作:redirect(action: "show", controller: "author", id: params.author.id)
关于grails - 如何使用grails中目标 Controller 的参数重定向到不同 Controller 的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41339617/