Skip to content

Instantly share code, notes, and snippets.

@pmcneil
Created January 25, 2011 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmcneil/794542 to your computer and use it in GitHub Desktop.
Save pmcneil/794542 to your computer and use it in GitHub Desktop.
This demonstrates a pattern suggested here http://bit.ly/h24hXy
package com.nerderg.test
class TestController {
def index = {
}
def changeColour = {
withPerson {
log.debug dump()
hairColour = 'blond'
render(view: "changeColour", model: [person: delegate])
}
}
def withPerson(id='id', Closure c) {
def person = Person.get(params[id])
if(person) {
c.delegate = person
c.resolveStrategy = Closure.DELEGATE_FIRST
c.call()
return person
} else {
flash.message = "The person was not found."
redirect action: "index"
}
}
}
class Person {
int height
int age
String sex
String hairColour
static constraints = {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment