我有一個兩個域類Author和Book相關爲一對多。在爲特定作者創建新書時,我想重新定向(在單擊保存按鈕時)到作者控制器的顯示操作,以便顯示特定作者的全部詳細信息並列出他創作的所有書籍。如何重定向到不同的控制器與grails中的目標控制器的參數的行動
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語句「書不是ID空發現」
以下是節目裏渲染這個_book.gsp模板作者類的.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
使用了'ID。 – doelleri
我將authorInstance.id更改爲params.id我認爲這應該可以工作,但它仍然產生這個Flash消息「Book not found with id null」,這是else語句的結果。我如何訪問正確的作者實例? –
'如果(params.tempid){ 重定向(動作: 「秀」,控制器: 「作者」,ID:params.id) }其他{ 重定向(動作: 「秀」,ID:params.tempid) } - 這對我來說很奇怪。如果tempid爲空或爲空,則使用此值重定向?你在這裏期待什麼?什麼是臨時的? –