Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't .collect() work in the following GString?

This works as expected in a GSP-page:

<td>${Foo.findAllByBar(bar)}</td>

But when adding a collect statement the code breaks ..

<td>${Foo.findAllByBar(bar).collect { it.name }}</td>

with

Error 500: Could not parse script [...gsp]: startup failed,
     ...: 129: expecting '}', found ')'
     @ line 129, column 196. 1 error`.

I was under the impression that any valid Groovy code could be placed in a GString ${ ... } and being correctly evaluated/expanded. What am I missing?

like image 473
knorv Avatar asked Dec 30 '22 11:12

knorv


2 Answers

Alternatively, you can use the spread operator:

<td>${Foo.findAllByBar(bar)*.name}</td>
like image 123
Matt Lachman Avatar answered Jan 06 '23 19:01

Matt Lachman


The GSP parser doesn't like } within the ${...} block. Try this one:

<%= Foo.findAllByBar(bar).collect { it.name } %>
like image 30
Siegfried Puchbauer Avatar answered Jan 06 '23 20:01

Siegfried Puchbauer