Saturday, 15 June 2013

groovy - SQL Query result not displaying in Grails view -



groovy - SQL Query result not displaying in Grails view -

i have followed several examples, can't seem final part of displaying query work. not getting errors, not displaying though query works in sql , know valid. can't figure out i'm missing. help appreciated.

my controller -

import groovy.sql.sql class v1controller { def datasource def index() { def namelist = { def db = new sql(datasource) def results = db.rows("select name table") //results.each{row -> log.debug .name} [results:results] } } }

my view -

<p> results: <g:each in="${results}"> <tr>row - <td>${results.name}</td></tr> </g:each> </p>

thanks responses, have tried these 2 ways , still not getting results in view.

<g:each in="${results}"> <tr>${it.name}</tr> </g:each> <g:each in="${results}" var="r"> <tr>name - ${r.'name'}</tr> </g:each>

thanks joshua! used recommendation on view, , had comment out other action name 'namelist', think realized trying create nested action , didn't need it. works now!

def index() { //def namelist = { def db = new sql(datasource) def results = db.rows("select name mit_test_name") //results.each{row -> log.debug .name} homecoming [results:results] db.close() //} }

there 2 issues here. first aren't using <g:each> tag correctly, , secondly property name isn't right according sql statement.

<g:each in="${results"} var="r"> <tr> <td>${r.'name'}</td> </tr> </g:each>

as can see above have give tag variable name (in case) knows how address individual element in scope of loop. picked r example. secondly, notice when access column name it's in single quotes , matches case of column returned in sql statement.

this should resolve issues.

grails groovy

No comments:

Post a Comment