Search code examples
grailsrendergsp

Rendering view, but no object in gsp


I try to render a view, which works fine, but it doesn't seem to get the model object I pass along to it. I can't figure out the reason, as this should be very straightforward according to all manuals and examples.

Model object

class Race {

    def distance = "1/4 mile"
    def racer1
    def racer2
}

RaceController renders here

def doFullRace(Race race) {

            render (view: 'raceProgress', model: [race: race])
        }

and raceProgress.gsp should display it easily enough

    <html>
    <body>
        <div id="raceStart" align="center">
...
            <p>${race.racer1} is racing ${race.distance} against ${race.racer2}</p>
        </div>
    </body>
    </html>

but I instead I get this object not found

Any ideas on what basic thing I missed?


Solution

  • You have the following:

    def doFullRace(Race race) {
        render (view: 'raceProgress', model: [race: race])
    }
    

    One of the ways for race to be null there is if all of the following are true:

    • Race is a domain class
    • The request submitted to doFullRace includes a request parameter named id
    • There is no record in the database with an id that matches params.id

    From http://docs.grails.org/3.3.9/guide/theWebLayer.html#commandObjects...

    If the command object’s type is that of a domain class and there is an id request parameter then instead of invoking the domain class constructor to create a new instance a call will be made to the static get method on the domain class and the value of the id parameter will be passed as an argument.

    And...

    If the command object’s type is a domain class and there is no id request parameter or there is an id request parameter and its value is empty then null will be passed into the controller action unless the HTTP request method is "POST", in which case a new instance of the domain class will be created by invoking the domain class constructor. For all of the cases where the domain class instance is non-null, data binding is only performed if the HTTP request method is "POST", "PUT" or "PATCH".