Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to return "render" in Grails?

Tags:

Is that return necessary? Let's say it's in the middle of an action.

render(contentType:'text/json', text: ['success': true] as JSON)    
return
like image 835
RyanLynch Avatar asked Jan 03 '12 15:01

RyanLynch


People also ask

What is render in Grails?

Description. A multi-purpose method for rendering responses to the client which is best illustrated with a few examples! Warning - this method does not always support multiple parameters. For example, if you specify both collection and model, the model parameter will be ignored.

What is respond in groovy?

Using the respond method to output JSON The respond method provides content negotiation strategies to intelligently produce an appropriate response for the given client. For example given the following controller and action: grails-app/controllers/example/BookController.groovy.


1 Answers

If you don't return, any code after render will also be executed, which is often not what you want, e.g.

def someAction = {

  if (someCondition) {
    render view: 'success'
    // if we don't return execution would fall through to the code below
    return  
  }

  log.error 'something went wrong'
  render view: 'error'
}

Of course, if you use this style instead, there's no need to return

def someAction = {

  if (someCondition) {
    render view: 'success'

  } else {    
    log.error 'something went wrong'
    render view: 'error'
  }
}

If an action only has one exit point, there's no need to return after render

def someAction = {
    render view: 'success'
}

Just remember that the code after render will be executed if you don't return.

like image 144
Dónal Avatar answered Oct 09 '22 14:10

Dónal