简体   繁体   中英

Grails default methods in controller

I am having a problem saving a domain instance in grails; The domainInstance that's passed to the default update method in my controller is NULL. The GSP page I am submitting from is not the default edit page. I have certain values from DB that need to be saved. I have the form tag defined on the page that contains the values I need to submit, as follows. <g:form id="sampleForm" url="[resource:domainInstance, controller:'cntrlrName', action:'update']" method="PUT" >

I also have a version field which looks like this. <g:hiddenField name="version" value="${domainInstance?.version}" />

My g:submit is as follows <g:actionSubmit action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" />

Why is my domain instance null? What am I missing?

This is the common mistake one could make. The attribute id in <g:form> tag is not the id attribute of HTML tags but it is the id to use in the link for default mapping of Grails ie

"/$controller/$action/$id" {}

So change your tag as:

<g:form name="sampleForm" id="${domainInstance.id}" controller="cntrlrName" action="update" method="PUT">

You can pass the domainInstance but I feel it is better practice to pass the id instead of the object. Try passing the id of the domain instance and then reading the object in the controller.

<g:form name="sampleForm" action="action" controller="controller" id="${domainInstance.id}" ></g:form>

// in controller

def resource = Domain.read(params.id)

another approach could be to pass the domainInstance as a hiddenField

<g:hiddenField name="resource" value="$domainInstance" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM