Grails - Controller chain and model disapearance
-
07-02-2021 - |
Question
I am trying to use the chain
feature to call an other method and render a merged model.
This is how I call the chain
:
Controller: emailToNotify, Method: sendEmailConfirmation
if (someCommandObject.hasErrors())
chain(controller: "creditProvider", action: "rank", model: [emailToNotify: epnc, somethingToDisplay: "Boom !!!!"])
This is how my rank
method end-up in my creditProvider
controller:
Controller: creditProvider, action: rank
render (view: 'simulateResult', model: [bim : bam, boom : bimbamboom])
I do not manage to call ${emailToNotify}
or ${somethingToDisplay}
from my simulateResult.gsp
view.
I have already used chain
before in this exact same way, and it was (and still is) working properly.
Any idea why the model is not passed properly to the view ?
Any help most welcome as always. All the best,
Solution
chain
only works if the final action returns a Map
and not if the final action uses the render
call. To get what you are doing to work you'll have to manually merge the implicit variable chainModel
with the model you're passing back in to the render
.
OTHER TIPS
I was having the same issue with this and found that one way to get around this is to use the Spring ModelAndView object. From the firstController:
chain(controller: second, action: 'chainedMethod', model: [modelField2: '2'])
Then in the secondController:
import org.springframework.web.servlet.ModelAndView
...
def chainedMethod() {
return new ModelAndView('/other/view', [modelField1: '1'])
}
This works for me using Grails 2.1.1 so can't speak to how well it works with other versions. Hope that helps other people, had to dig around and try a few different routes before getting this.