I’m new to Grails and now I’m trying to retrieve objects/model from a controller to a template using AJAX. I want to make it so that sms properties would accessible to messageBox template, but this would always return me a null value. Could anyone help me with this? Any answer would be appreciated, here’s my current code.
On my client
<g:form>
<label for="id">Sms ID </label>
<g:textField name="id" />
<g:submitToRemote value="search" update="msgBox"
url="[controller:'sms', action:'send']"/>
</g:form>
<g:render template="messageBox" model="${[sms:sms]}/>
My controller
SmsController{
def send = {
def sms = new Sms(...)
//assume properties have been set
...
...
render(template: messageBox, model:[sms:sms])
}
}
and my _messageBox.gsp
<div id="msgBox">
<span>Sms Property 1: ${sms?.property1}</span>
<span>Sms Property 2: ${sms?.property2}</span>
<span>Sms Property 3: ${sms?.property3}</span>
</div>
There seems to be a few logistical errors here
First, you seem to be rending the messageBox template twice. In your ‘client’ gsp you are calling…
On page load ‘sms’ will always be null unless you are provided one on the page load. Then you also seem to be calling it again in your controller…
Also, I would move the div:’msgBox’ outside of the template and into your client something like…
Lastly, make sure you have a javascript library in your header like prototype or jquery. I’m not sure this will solve your problems but it will be a good start. Let me know!