shoutian
shoutian

Reputation: 11

Grails formRemote redirects rather than to just call method

I'm new to Grails and got some problem with the g:formRemote command..

I want a g:textArea box send a message to my controller and save this messages. After that the page should be updated via the formRemote Ajax, so that the messages appear on the page.

But instead of updating the page, the formRemote call assumes the given url to be a real link and wants me to redirect to this (non-existing) .jsp site.
The Method I want to start is called in my controller tho

I tried many solutions offered in similar problems, but it seems this problem is different from theirs

Heres the code:

<div id="history">
    <g:render template="posts" collection="${ messages }" var="message" />
</div>
<div class="postMessageForm">
    <g:formRemote name="postChatMessage" url="[controller: 'meetingRoom', 
                  action: 'postMessage']" update="history">                             
    <div class="msg_box">
        <g:textArea name="message" value="" style="width: 630px"/><br/>
    </div>
    <div style="float: right;">
        <g:submitButton name="Send" style="width: 90px; height: 40px;"/>
    </div>
    </g:formRemote>
</div>

and this is the Action which is called in my MeetingRoomController:

def postMessage() {
if (params.message != "") {
    def thisUser = lookUpUser()
    def thisRoom = thisUser.joinedRoom
    def chatPost = new ChatPost(
        message: params.message,
        author: thisUser
    )
    thisRoom.addToChatHistory(chatPost)
}
//  def messages = currentChatHistory()
//  render template: 'posts', collection: messages, var: 'message'

I saw this kind of approach in Jeff Browns Twitter tutorial.

Possible failures i am seeing:

I hope this information is enough to let someone see what I am missing

Upvotes: 1

Views: 1084

Answers (1)

John Moses
John Moses

Reputation: 1283

When the submit button is clicked on your form, the data is sent to the method listed in the url parameter of the formRemote tag. Then you are inside that method, you get to the commented out render tag that outputs data back to the gsp page in the div mentioned in the update tag of the formRemote tag.

formRemote relies upon a javascript library to handle the ajax stuff as mentioned in the grails documentation:

7.7.1 Ajax Support

By default Grails ships with the jQuery library, but through the Plugin system provides support for other frameworks such as Prototype, Dojo:http://dojotoolkit.org/, Yahoo UI:http://developer.yahoo.com/yui/ and the Google Web Toolkit. This section covers Grails' support for Ajax in general. To get started, add this line to the tag of your page:

You can replace jQuery with any other library supplied by a plugin you have installed. This works because of Grails' support for adaptive tag libraries. Thanks to Grails' plugin system there is support for a number of different Ajax libraries including (but not limited to):

jQuery Prototype Dojo YUI MooTools

So remove what is in the history div, uncomment the two lines in your postMessage method, and include one of the referenced javascript libraries.

Upvotes: 1

Related Questions