同じフォームとアクションを使用して、作成 (保存) と編集 (更新) の両方を行うつもりですか?
ある時点save
でのアクションは、保存が成功したかどうかに基づいて、redirect
または特定のビューになります。render
保存するかどうかに関係なく、常に同じビューをレンダリングする必要があるため、次のようにします。
def save = {
def propertyInstance
//you need to do this since you are both saving and updating in the same action
if(params.id) {
propertyInstance = Property.get(params.id)
propertyInstance.properties = params
} else {
propertyInstance = new Property(params)
}
if (propertyInstance.save(flush: true)) {
flash.message="Property ${propertyInstance?.id} : ${propertyInstance?.address} has been added successfully"
}
else {
flash.message = "Please enter details again"
}
render(view: "view", model: [propertyInstance: propertyInstance])
}
そして、次のようview.gsp
に設定したものを表示できます。flash.message
<g:if test="${flash.message}">
<div class="message">${flash.message}</div>
</g:if>
編集
テンプレート (_addressMessage.gsp
たとえば、と呼ばれる) を使用して、いくつかのフォーマット (別々の行にある住所の場合など) でview.gsp
メッセージを表示したい場合は、メッセージを表示したい場所で次のようなことを行うことができます。
<g:if test="${propertyInstance.address}">
<g:render template="addressMessage" model="[propertyInstance: propertyInstance]" />
</g:if>
<g:else>
Please enter details again.
</g:else>
<g:if...
住所がない場合はこれを表示したくないと思うので、そこに を含めました。