I would like to ask if its possible to have a closure to the rendering form and validation of inputs then let the other controller to the persistence? I am pertaining to this line of code below:
def create() {
if(params.size() <= 2) {
[modelInstance: new <Domain>()]
} else {
def model = new <Domain>(params)
if(!model.validate()) {
render(view:"create", model: [modelInstance: model])
return
} else {
chain(action:"save", model: [modelInstance: model])
}
}
}
def save() {
// where the modelInstance object was created from the create closure
if (!modelInstance.save(flush: true)) {
render(view: "create", model: [modelInstance: modelInstance])
return
}
flash.message = "Sucess!"
redirect(action: "show", model: [modelInstance: modelInstance])
}
Note: The second closure
savedoesn’t have a .gsp file to render, what it
only does is do the persistence then redirect the process either pass
or fail.
When I use the first modelIstance in the save closure, the page return an HTTP 405 error. This is a result because we couldn’t locate the model object, yet we used the chain method?
It looks like you’re trying to use chain where a private helper method would be more appropriate. Try encapsulating the logic you need in both controllers in a separate method.