Michal_Szulc
Michal_Szulc

Reputation: 4177

Grails 3.2.0.M1 Template not found for name

In my domain class com.example.users.User I added transient field carnets:

class User implements Serializable {
    ...
    def carnets

    static transients = ['springSecurityService', 'carnets']
    ...
}

and in my gson view user/_user.gson I'd like to render it:

import com.example.users.User

model {
    User user
}

json g.render(user, [excludes:['password', 'deleted', 'enabled', 'accountExpired', 'accountLocked', 'passwordExpired', 'authorities']]) {
    //"carnets" g.render(template:"/carnet/index", collection: user.carnets, var:'carnets')
    "carnets" tmpl.'/carnet/index'(user.carnets)
}

but I've received:

Caused by: grails.views.ViewRenderException: Error rendering view: Template not found for name /carnet/index

Carnet's views gson files were autogenerated and works fine when executed from CarnetController.

What am I missing?

Upvotes: 4

Views: 730

Answers (2)

RMorrisey
RMorrisey

Reputation: 7739

In my use case (Grails 3.3.0), I had to change the template path from: tmpl.'message/message' to: tmpl.'/message/message' (added leading slash).

Using the ../ syntax worked in development, but caused an error for me when deploying the WAR file to Tomcat. See: [https://github.com/grails/grails-views/issues/140]

Upvotes: 8

Joshua S.
Joshua S.

Reputation: 153

You need to use a relative path to the template from the current .gson file, unfortunately. I would imagine that that option would be an relative path from the views folder, but it's apparently not the case.

Let's assume your views folder structure is as follows:

views/
    carnet/
         _index.gson
    user/
        _user.gson

In this case, your _user.gson file needs to be as follows:

import com.example.users.User

model {
    User user
}

json g.render(user, [excludes:['password', 'deleted', 'enabled', 'accountExpired', 'accountLocked', 'passwordExpired', 'authorities']]) {
    "carnets" tmpl.'../carnet/index'(user.carnets)
}

TL;DR change tmpl.'/carnet/index' to tmpl.'../carnet/index'. :)

Upvotes: 1

Related Questions