4

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?

Michal_Szulc
  • 4,097
  • 6
  • 32
  • 59
  • Step one should obviously be to upgrade to a newer release than the first milestone release of the major version you're using, in this case 3.2.2 – Burt Beckwith Nov 06 '16 at 04:07
  • 1
    I tried it: http://stackoverflow.com/questions/40441814/second-level-expand-in-gson/40462611#40462611 but thanks for a hint. – Michal_Szulc Nov 10 '16 at 10:56

2 Answers2

8

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]

RMorrisey
  • 7,637
  • 9
  • 53
  • 71
1

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'. :)

Joshua S.
  • 133
  • 9