I’m new to Grails, and I have a question that should be easy for most of you.
I have a page displaying an object
list. I want to display a message if there’
The flash
object is a Map
which stores key/value pairs, so you can define your own key for error messages. For example:
try {
instanceToDelete.delete(flush: true)
flash.message = "successfully deleted object"
}
catch (DataIntegrityViolationException e) {
flash.error = "could not delete object"
}
redirect(action: "list", id: params.id)
Then you can check the flash
object containing the error
key, and use a different style for that kind of message:
<g:if test="${flash.error}">
<div class="alert alert-error" style="display: block">${flash.error}</div>
</g:if>
<g:if test="${flash.message}">
<div class="message" style="display: block">${flash.message}</div>
</g:if>
This can help you:
def delete() {
def instanceToDelete= Myobject.get(params.id)
try {
instanceToDelete.delete(flush: true)
flash.success = "Object deleted correctly"
} catch (DataIntegrityViolationException e) {
flash.error = "Something goes wrong"
}
redirect(action: "list", id: params.id)
}
redirect to the gsp after all the code, to can store if there is an error or everything goes well.
you can put the messages in different variable to discriminate between error and success.
<g:if test="${flash.success}">
<div class="alert alert-success" style="display: block">${flash.success}</div>
</g:if>
<g:if test="${flash.error}">
<div class="alert alert-error" style="display: block">${flash.error}</div>
</g:if>
// backend code example
def save () {
if(params.name) {
.
.
object.save();
flash.message = "Saved successfully"
}
else {
flash.message = "Saved fail"
}
// HTML example
<g:if test="${flash.message}">
<div class="update_message" role="status">${flash.message}</div>
</g:if>
Strict answer : Just return your message (or render with a model map)
for your controller :
def delete() {
def instanceToDelete= Myobject.get(params.id)
try {
instanceToDelete.delete(flush: true)
redirect(action: "list", id: params.id)
}
catch (DataIntegrityViolationException e) {
render view:'delete', model:[message: "some message"]
//I want to refresh the div containing the flash.message here
}
}
for your gsp :
<g:if test="${message}">
<div class="alert alert-error" style="display: block">${message}</div>
But Gregg is right, you should never modify client data without a redirect
.
If you do, the user might refresh (or come back to) the same url, and attempt again same action accidentally. You should really do like in hitt5's answer.