如何使用grails中目标控制器的参数重定向到不同控制器的操作

时间:2016-12-27 05:41:47

标签: grails

我有两个域类作者和书籍相关为1对多。在为特定作者创建新书时,我想将(在单击保存按钮时)重定向到作者控制器的show动作,以便显示特定作者的完整详细信息以及他创作的所有书籍的列表。 / p>

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)
    }
}

但是上面的代码并没有像我想的那样重定向我,而是使用flash消息“找不到id为null的书”来执行else语句

下面是在Author class的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

1 个答案:

答案 0 :(得分:0)

如果要在保存book后重定向到作者实例,则应在保存请求参数中发送作者id。默认情况下,作者ID包含在params地图中author.id。 所以这应该工作:
redirect(action: "show", controller: "author", id: params.author.id)

相关问题