I am trying to pass a collection from my controller to view like so:
def index() {
childInstance = Child.get(params.id)
if(childInstance){
System.out.println("CHILD" + childInstance.firstname)
def messages = currentUserTimeline(childInstance)
[profileMessages: messages, childInstance: childInstance]
}
else{
def messages = currentUserTimeline(null)
[profileMessages: messages]
System.out.println("ALL " + messages)
}
}
The if works but the else docent it sends profileMessages as a null object.
if i add
render template: 'profileMessages', collection: messages, var: 'profileMessage'
to the else this works but i want to pass everything to the view and not render it in the controller.
in the view i am using :
<g:render template="profileMessages" collection="${profileMessages}" var="profileMessage"/>
Any ideas why it works in the if and not in the else when sent to the view?
FYI i have added
[profileMessages: messages, childInstance: null]
to the else with no luck and null is allowed and does work in the
currentUserTimeline(null)
because
render template: 'profileMessages', collection: messages, var: 'profileMessage'
works.
The map with the model must be the last commandin the block, so move the System.out.println() above the map.
And it also helps, if you use:
BTW … use log4j instead of System.out.println 😉