Grails填充域实例

时间:2012-09-10 19:29:23

标签: grails

我在同一个问题上被困了几天而且我没有到达任何地方。

我正在使用g:formRemote标记来更新消息模板

<g:formRemote url="[action: 'updateStatus']" update="messages" name="updateStatusForm"
                    onSuccess="document.updateStatusForm.message.value='';">
                    <g:textArea name="message" value="" cols="3" rows="1"/><br/>
                    <g:submitButton name="Update Status"/>
                </g:formRemote>

然后,此表单在我的控制器中调用updateStatus方法

def updateStatus(String message) {
        def status = new Post(message: params.message, author: lookupPerson())
        status.save(flush: true, failOnError: true)
        def messages = currentUserTimeline()
        render template: 'profileMessages', collection: messages, var: 'profileMessage'
    }

但是def状态行应该是

def status = new Post(message: params.message, author: lookupPerson(), child: Child.get(childInstance.id)

在我看来,我有一个由用户选择的childInstance。无论如何我可以在控制器中保存一个全局变量,或者我可以在formRemote标签中发送子实例params id。我不认为有这样的事情我今天一直都是这样做的。

任何帮助都会很好。我知道唯一可行的方法是将childInstance.id写入属性文件并将其读回,但必须有更好的方法,因为我认为这将是非常糟糕的做法。

2 个答案:

答案 0 :(得分:1)

您可以将参数添加到<g:formRemote>

grails docs中的示例:

<g:formRemote name="myForm" update="updateMe"
              url="[controller: 'book', action: 'byAuthor', params: [sort: 'title', order: 'desc']]">
    Author: <input name="author" type="text" />
</g:formRemote>

答案 1 :(得分:0)

您可以在formRemote标记中添加<g:hiddenField name="childId" value="${childInstance.id}"/>,这将以与消息相同的方式将其发送到服务器。

相关问题