开发者

Grails - Many-to-Many search

开发者 https://www.devze.com 2022-12-22 07:19 出处:网络
I have two domains declared in my app. class Posts { String title String content static hasMany = [tags:Tag]

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.


Replace

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

by

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

精彩评论

暂无评论...
验证码 换一张
取 消