-
Dec 29th, 2010, 03:13 PM
#1
Grails elvis operator
Hello
I am new to Grails and Groovy. I am currently walking through one of the tutorials which introduces you to grails plugins. Searchable plugin is the one used as an example but on installing the plugin sucessfully I noticed that when using the search tool to search my classes and leaving the input field blank it gives me a <EOF> found error.
Not a problem as I then proceeded to adapt the search method within my class controller from:
def search = {
flash.message = "Search results for: ${params.q}"
def resultsMap = Race.search(params.q, params)
render(view:'list', model:[
raceInstanceList:resultsMap.results,
raceInstanceTotal:Race.countHits(params.q)
])
}
to:
def search = {
def qStr = params.q != "" ? params.q : "*"
flash.message = "Search results for: $qStr"
def resultsMap = Race.search(qStr, params)
render(view:'list', model:[
raceInstanceList:resultsMap.results,
raceInstanceTotal:Race.countHits(qStr)
])
}
This works fine but I was hoping to use a more elegant solution. Can anyone help with the following points:
1. Can I improve the elvis operator? I tried the below instead of the above but it did not work. I thought that it would implicitly apply the params.q to my qStr variable if the condition was true?
def qStr = params.q != "" ?: "*"
2. How can I perform a regExpression and check for all non-expected entries. I.e. If I enter a space into the field this also causes an error.
Look forward to some Guru to give me advise. Please be gentle as I'm a beginner. No wacky solutions please :-)
Kind regards
Andyrh3
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules