Grails render view of different controller

Sap picture Sap · Feb 7, 2012 · Viewed 21.5k times · Source

My question is similar to this following post Render a view of another controller

I have a TestConfigController my question is what can I do in case validation fails and I want to render controller:test and view:edit rather then controller:testCOnfig and view:edit

def save() {

        def testConfigInstance = new TestConfig(params)
        if (!testConfigInstance.save(flush: true)) {

/*Rather then view:"edit" i want view:"/test/edit" which does not work */

            render(view:"edit",  model: [testConfigInstance: testConfigInstance],id:params.test.id)
            return
        }
        println "+++++++++++++++++++++++++"
        flash.message = message(code: 'Data successfully saved', args: [message(code: 'testConfig.label', default: 'Successfully saved')])
        redirect(action: "edit", controller:"test", id:params.test.id)
    }

Any pointers? I have already looked into grails redirect which does not have "model" param and thus can not pass the validation errors to the view Also I have looked in to grails render which does not have controller param so that I can go back to different controller! Please let me know if more detail/code is needed

EDIT Following happens while using one of the two things

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance],id:params['test.id'])

The code above renders the page /test/edit with no reference to testid eventually erroring out saying "test.id" can not be null.. (means its rendering /test/edit and not /test/edit/1)

render(view:"/test/edit/"+params['test.id'],  model: [testConfigInstance: testConfigInstance],id:params['test.id'])

The code above leads to following error

The requested resource (/EasyTha/WEB-INF/grails-app/views/test/edit/1.jsp) is not available.

Either one of the above code renders just "/test/edit" no id at the end, thus eventually erroring out saying test.id can not be null.

Answer

Anuj Arora picture Anuj Arora · Feb 10, 2012

The value of id that you are trying to append in the view path should be a part of the model map. The values that you provide in the model map are available in the view that is rendered.

In the first option that you tried, the id parameter doesn't make any difference as the render method doesn't use any 'id' parameter (the redirect method uses the id parameter to generate the redirect url).

Your code snippet should be something like this:

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance, id:params['test.id']])

The render method that you are using here doesn't redirect you to some other action. render just prints the parsed viewName to the output stream. Eg. render(view:"/test/edit") just renders the edit.gsp view. It isn't actually redirecting you to the edit action of test controller. Therefore, just passing the id in the model map won't give you access to the testInstance on the view. You will have to get the testInstance By id and pass it to the view in the model map

render(view:"/test/edit",  model: [testConfigInstance: testConfigInstance, testInstance: Test.get(params['test.id'] as Long)])