tags:

views:

48

answers:

1

I have two domains declared in my app.

class Posts {

   String title
   String content
   static hasMany = [tags:Tag]

   static constraints = {
   }
}

class Tag {

    String Name
    static belongsTo = Post
    static hasMany = [posts:Post]
    static constraints = {
    }

    String toString()
    {
        "Tag:${Name}"
    }
}

I have a controller which manages the search and display of results:

package com.trafigura.com.trafigura

class ViewerController {

    def defaultAction='search'
    def search={}

    def show = {
        def _foundPost = Post.findAllBytitle(params.title)
        return [posts: _foundPost, term: params.title]
    }
}

The search.gsp code is as follows:

<html>
  <head><title>Simple GSP page</title></head>
  <body>Place your content here.

      <formset>
        <legend>TagsPosts</legend>
          <g:form action="show">
            <label for="title">Title</label>
            <g:textField name="title" />
            <g:submitButton name="search" value="Search"/>
          </g:form>
      </formset>
  </body>
</html>

and the following code for show.gsp.

<html>
  <head><title>Simple GSP page</title></head>
  <body><h1>Results</h1>
    for items matching <em>${term}</em>.
    Found <strong>${posts.size()}</strong> hits.
    </p>
  <ul>
    <g:each var="tag" in="${posts.tags}">
      <li>${tag.Name}</li>
    </g:each>
  </ul>

  <g:link action='search'>Search Again</g:link></body>
</html>

My Question is I am unable to display the tags as follows:

Results

Found 1 hits.

* [planting, dig]

However, I want the output as:

* planting
* dig

What am I doing wrong here?

Much Appreciated.

+1  A: 

Replace

<g:each var="tag" in="${posts.tags}">

by

<g:each var="tag" in="${posts.tags[0]}">
Philippe
Hi Phil, thanks for ur reply. Indeed it worked. Howver, now one more thing whenever I run the above code it returns me tags in a random order, how can I sort them?Thanks,Much Appreciated
WaZ
In your Post domain class, declare your tags field as a SortedSet. See the second example for the Author class in this example : http://www.grails.org/GORM+-+Collection+Types
Philippe