Monday, November 2, 2015

Grails findAllBy Method

Home with a sick kid today, figured I could cobble together a video and a blog post. I am writing this one-handed, with a sick kid on my lap, watching PJ Masks, truly a great day. See video for a demo of the findAllBy method. You can use this method to create a pared down list, params.id is passed from URL. Kind of a cool method you may end up using on the reg.


GGTS 3.6.4 and Grails 2.4.4



Walk thru: 
  1. Start by creating a new project named lameProj
  2. Then create a domain class FavColor.groovy
package lameproj

class FavColor {
     String name
     String favColor

    static constraints = {
    }
}

   3.  Then generate-all lameproj.FavColor
   4.  Copy views >favcolor > index.gsp
   5.  Paste and rename to youtube.gsp
   6.  Add below code to favColorController.groovy

     def youtube(Integer max) {
          params.max = Math.min(max ?: 10, 100)
          def somelist = FavColor.findAllByFavColor(params.id)
          respond FavColor.list(params), model:[favColorInstanceCount: FavColor.count(), somelist:somelist]
     }


   7.  Edit youtube.gsp to use ${somelist}, about line 34

<g:each in="${somelist}" status="i" var="favColorInstance">


   8.  Launch you app, add some values, ensure the favorite color entries are exact. Test your youtube method with this link: http://localhost:8080/lameProj/favColor/youtube/Blue


No comments:

Post a Comment