Grails render view of different controller

21,809

Solution 1

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)])

Solution 2

Anuj Arora is right:

If you just want to render an arbitrary view you can use the full path to the view related to the grails-app/view folder:

In your case:

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

should work.

Solution 3

If you are only wanting to render the view /test/edit, then the call render(view:'/test/edit',...) should be all you need.

If instead, you also want to include some of the processing from the TestController and edit action, then look at the chain() call. It has a model parameter where you can pass the validation errors and controller/action parameters to redirect to the other controller.

Share:
21,809
Sap
Author by

Sap

There is nothing much I can say

Updated on July 09, 2022

Comments

  • Sap
    Sap almost 2 years

    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.

    • Anuj Arora
      Anuj Arora about 12 years
      The render closure with view "/test/edit" should be working. Which version of grails are you using?
    • Jan Wikholm
      Jan Wikholm about 12 years
      Could you provide more information on "does not work"? Do you get some error message? It should work.
    • Sap
      Sap about 12 years
      @AnujArora Grails 2.0. It's now working but now thinking of it I have a feeling that it might not be working because of development context? May be I should try /context/test/edit
    • Anuj Arora
      Anuj Arora about 12 years
      @Grrrrr You don't need to prepend the context on the view name. Grails should handle that.
    • Sap
      Sap about 12 years
      @AnujArora sorry for a long absence, please see my edit
    • Sap
      Sap about 12 years
      @JanWikholm sorry for a long absence, please see my edit
  • Peter
    Peter about 12 years
    The con of the chain() call is that a redirect is sent to the client. This adds overhead to application. In this case I would prefer the include() call.
  • schmolly159
    schmolly159 about 12 years
    True, you could render the result of a g.include() call and save the redirect overhead. Had never considered that option.
  • Sap
    Sap about 12 years
    Hey chain worked perfectly, still wondering how would include work in such a scenario where test has many testconfig and I am showing all testconfigs in test/edit/..
  • schmolly159
    schmolly159 about 12 years
    Since it is a Tag Library it is intended to be used in a GSP (for rendering templates instead of <g:render/>), but you can call them in a Controller, so you could try doing something like render(g.include(controller:'test',action:'edit',model:...))‌​.
  • Sap
    Sap about 12 years
    This solution doesn't seem to be working the webpage renders with just test/edit and no reference to Id... my gut feeling is that it is due to the fact that model has testConfig instance while we are redirecting to test/edit?
  • Anuj Arora
    Anuj Arora about 12 years
    The id should be available as a model parameter in the /test/edit view. Can you post your gsp here?
  • Sap
    Sap about 12 years
    too big a GSP to paste here. if you don't mind can you download it? cynosuredev.com/edit.rar
  • Anuj Arora
    Anuj Arora about 12 years
    @Grrrrr please see my edit. I think you are getting confused between redirect and render. This post might help you grails.1312388.n4.nabble.com/…
  • Sap
    Sap about 12 years
    Thanks a lot, this works. I understand that render is equivalent to forward in java while redirect is to redirect:) Anyways, thanks a lot for your help it never occurred to me that I can send two objects in a model.