Grails/AJAX: Updating an arbitrary region in the page using g:submitToRemote

ajax, grails, gsp

Solution

According to my testings, `g:submitToRemote`'s `action` attribute must not point to the current controller's current action (as this will insert/duplicate the current view into the current view).

It works if you specify an alternate action in `g:submitToRemote` - i.e.,

<g:submitToRemote value="Click Me"
    action="ajaxAction" update="updateMe" />

If this action provides a model - i.e.,

def ajaxAction = { [message: 'foo'] }

then there needs to be a corresponding GSP - that, in this case, should state,

$message

Alternatively, the action can use the `render` method - like this,

def ajaxAction = { render 'foo' }

I'll leave this issue open for some time, in case there might be additional responses, and, if there aren't, will accept this answer as the solution.

Thanks

Problem

In a GSP (Groovy Server Page), I'm using `<g:submitToRemote update="...">` to update a `<div>` after the server-side call. According to the tag's documentation and other sources on the web, the target `<div>` can be placed arbitrarily at the page. In my testings, however, I find that the `<div>` needs to surround the `<g:submitToRemote>` tag. If it does not, the `<div>` will be updated with some "random" contents (i.e., parts of the form that surround the `<g:submitToRemote>` tag). Consider the following GSP code: ``` <html> <head> <g:javascript library="prototype" /> </head> <body> <div id="updateMe_NOT_WORKING">${message}</div> <g:form> <div id="updateMe_WORKING"> <g:submitToRemote value="Click Me" action="someAction" update="updateMe_NOT_WORKING" /> </div> </g:form> </body> </html> ``` That's on Grails 1.3.4. What am I missing? - Thanks

Original source