Search code examples
grailsgsp

a List of objects disappeared after Rendering a view


Dear grails specialists

I'm sending a list of publications to my gsp view page. I can see the list well, but after adding one of the publications into the books list and rendering the publications gsp view page, the publications list is not seen as a list of publications but as a String. So when I try to get each publication it gives me the chars.

I am using the version of grails 2.2.0.

Here i send publications in controller Entries:

def books{
def unit = Unit.get(params.id)
def period = Period.get(params.periodId)
def publications = []
if (unit) {
            def units = unit.downHierarchy().id
            units.removeAll { null }
            publications = Publication.publicationsOfUnits(units)?.listDistinct()?.findAll{it.date.getAt(Calendar.YEAR) == period.year}?.sort{ it.date }
        }
[publications : publications, id: unit.id, unitId: unit.id, periodId: period.id]
}

and here is my view page:

    <table>
        <g:each in="${publications}" var="p">
          <g:if test="${p instanceof publicationBook}">
           <tr>
            <td>${p.authors.join(",")}</td>
            <td>${p.title}</td>
            <td>
            <a href="#" onclick="$('#author').val('${p.authors.join(',')}');$('#titleAndSubtitle').val('${p.title}');$('#unitId').val('${unitId}');$('#periodId').val('${periodId}');$('#id').val('${unitId}');bookForm.submit();">Add to Books</a>
            </td>                   
            </tr>                   
           </g:if>
        </g:each>
</g:table>

here i add the publication to books:

def saveBook = {
    saveEntry("book", new Book())
    render(view:"books", model: params)
}

and here is the book Form where I send the list of publications again to controller for rendering:

<g:form name="bookForm" controller="entries" action="saveBook" class="default" method="post">
<g:hiddenField name="entryId" value="${entry?.id}"/>
<g:hiddenField name="unitId" value="${unitId}"/>
<g:hiddenField name="periodId" value="${periodId}"/>
<g:hiddenField name="publications" value="${publications}"/>
<div class="buttons">
<button class="button icon icon_arrow_right leave_empty" onclick="$('#leaveEmpty').val('true');bookForm.submit();" 
</div>
</g:form>

I would very appreciate any advise or help to solve the problem..


Solution

  • You are NOT passing the proper model here render(view:"books", model: params).

    Either use redirect(action:"books", id: id), or fill the model with the publications list etc., as you are doing in the books action