Sagarmichael
Sagarmichael

Reputation: 1624

pass from controller to view in grails

I am trying to pass from my controller to my view in grails In my controller i have this:

    def index() {
    def user = User.get(springSecurityService.principal.id)
    def children = user.children
    }

and in my gsp (view i have this)

 <g:each var="child" in="${children}">
    <p>Name: ${child.firstname}</p>
    <p>Author: ${child.lastname}</p>
 </g:each>

It does not pick up the children object from my controller i have also tried

<% def children = children %>

with no luck

can some one please point me in the right direction

Thanks

Upvotes: 0

Views: 4507

Answers (2)

doelleri
doelleri

Reputation: 19682

By convention, Grails will render the view that matches the controller action that was executed. It will also, by convention, pass as the return value of the action as the view's model. In your case for this to work correctly, you can either explicitly render the view as in @Mr. Cat's answer, or you can return the model like

def index() {
    def user = User.get(springSecurityservice.principal.id)
    [children: user.children]
}

Groovy implicitly returns the last expression evaluated.

Upvotes: 5

Mr. Cat
Mr. Cat

Reputation: 3552

Try this:

def index() {
   def user = User.get(springSecurityService.principal.id)
   render(view:'index',model:[children:user.children]) 
}

Upvotes: 4

Related Questions