Grails - Controller chain and model disapearance
Asked Answered
F

2

5

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,

Footstool answered 5/10, 2011 at 16:29 Comment(0)
O
11

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.

Oxytocin answered 5/10, 2011 at 16:37 Comment(2)
Thank you, you have ended-up 2hours of tracing variables :) Is this written somewhere in the documentation ?? I did not read this there, but maybe I missed it.Footstool
I don't know of any specific documentation on that rule. I don't remember where I first learned it either.Oxytocin
C
1

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.

Chieftain answered 5/3, 2013 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.