grails delete.dispatch … have you seen this?


I recently created a custom list page for a domain. I wanted to be able to delete an individual item on that list by using

<g:link action="delete" params="&#91;id:domainObject.id&#93;">delete</g:link>

found that I kept getting a

RequestURI=/myproject/grails/domainObject/delete.dispatch error.

I soon discovered that this is caused by:

def allowedMethods = [delete:'POST', save:'POST', update:'POST']

The allowedMethods being limited to POST which is good. Now, how can I work around this to delete an item within a list without changing my allowedMethods.

I tried numerous things including trying to insert a method=”post” within the tag… which I know is just dumb…but was desperate.

What I found was 2 working solutions. The first solution is to continue using the tag.

<g:form method="post" name="domainObjectForm">
<input type="hidden" name="domainObjectId" value="">
<table>
<g:each in="${domainObjects}" status="i" var="domainObject">
<tr>
<td>${domainObject.awesomeThing}</td>
<td><g:link onclick="javascript:deleteDomainObject(${domainObject.id}, 'delete');">delete</g:link></td>
</tr>
</g:each></table>
<script type="text/javascript">
    function deleteDomainObject(domainObjectId, action){
         document.domainObjectForm.domainObjectId.value=domainObjectId;
         document.domainObjectForm.action = action;
         document.domainObjectForm.submit();
    }
</script>
</g:form>

As you can see, used a little javascript to name the form action and to specify the appropriate id. Of course you will need to adjust the Controller delete method to grab the params.domainObjectId instead of the params.id.

There is one problem with this.. by using the g:link you will lose the flash.message functionality.

To work around this, you will need to use the like this:

<g:form method="post" name="domainObjectForm">
<input type="hidden" name="domainObjectId" value="">
<table>
<g:each in="${domainObjects}" status="i" var="domainObject">
<tr>
<td>${domainObject.awesomeThing}</td>
<td><g:submitButton name="delete" value="delete" onclick="javascript:deleteDomainObject(${domainObject.id}, 'delete');"/></td>
</tr>
</g:each></table>
<script type="text/javascript">
    function deleteDomainObject(domainObjectId, action){
         document.domainObjectForm.domainObjectId.value=domainObjectId;
         document.domainObjectForm.action = action;
         document.domainObjectForm.submit();
    }

</script>
</g:form>

This will allow you to continue to use the flash.message ability as well as get rid of the delete.dispatch problem

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s