Question

When I generate a controller for a domain class named User I get this code for the edit action:

def edit(Long id) {
    def userInstance = User.get(id)
    if (!userInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
        redirect(action: "list")
        return
    }

    [userInstance: userInstance]
}

Can someone please explain why this returns [userInstance: userInstance], rather thank just userInstance

Thank you!

Was it helpful?

Solution

The controller returns a map containing the data that you will (presumably) use in your view. The map keys are the names you use to this data from your view. Perhaps it would be a little less confusing if you renamed the map key, e.g.

def edit(Long id) {
    def userInstance = User.get(id)
    if (!userInstance) {
        flash.message = message(code: 'default.not.found.message', args: [message(code: 'user.label', default: 'User'), id])
        redirect(action: "list")
        return
    }

    [user: userInstance]
}

To get the id of the User in your view, you would use:

${user.id}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top