FrancescoDS
FrancescoDS

Reputation: 995

grails exception: Tag [paginate] is missing required attribute [total]

I've the following method in my controller, that is used to list the Patient entities:

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)


    def roles = springSecurityService.getPrincipal().getAuthorities()
    if(roles == 'ROLE_USER')
    {
        [patientInstanceList: Patient.findAllBySecUser(springSecurityService.getCurrentUser()), patientInstanceTotal: Patient.count()]
    }
    else if(roles == 'ROLE_ADMIN' || roles == 'ROLE_MANAGER')
    {
        [patientInstanceList: Patient.list(params), patientInstanceTotal: Patient.count()]
    }
}

If I perform this method I have the exception

Tag [paginate] is missing required attribute [total]

But, if I use the following

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)
    [patientInstanceList: Patient.list(params), patientInstanceTotal: Patient.count()]
    }
}

Everything works well. I see all the instances of Patient created. I only want that, based on role of user logged in, I can see a part of all the entities created. Why does this exception is raised?

I've found here Grails pagination tag error something similar, but no good answer was given

Upvotes: 0

Views: 1306

Answers (1)

user800014
user800014

Reputation:

I'm not sure that you can ommit the return in a if statement. Another detail is that you're filtering your domain records in the ROLE_USER, but counting the total independent of this filter.

You can try this approach:

def list(Integer max) {
    params.max = Math.min(max ?: 10, 100)

    def roles = springSecurityService.getPrincipal().getAuthorities()

    def model

    //I think roles is a list so your equals will not work...
    def admin = roles.find { it.authority == 'ROLE_ADMIN' || it.authority == 'ROLE_MANAGER' }
    if(admin) {
       model = [patientInstanceList: Patient.list(params), patientInstanceTotal: Patient.count()]
    } else {
      //createCriteria().list(params) will paginate...  
      def patientInstanceList = Patient.createCriteria().list(params) {
        eq('secUser', springSecurityService.currentUser)
      }
      //totalCount will trigger a query without the limit, but applying your filter
      model = [patientInstanceTotal: patientInstanceList.totalCount, patientInstanceList: patientInstanceList]
    }

    //and finally we return the model
    //return model
    //just to show that the return is optional

    model

}

Upvotes: 1

Related Questions