Search code examples
grailsgrails-orm

How to modify g:form tag when ID has a different name in show.gsp to make delete work?


My Domain class ID is having mapped to bookId

class Book {
    int id
    int bookId
    String bookAuthor

    static constraints = {
        bookAuthor blank: false, maxSize:30         
    }

    static mapping = {    
        version false
        id generator: 'sequence',
        params: [sequence:'s_book_seq'],
        name: 'bookId'
    }
}

That caused the generated show.gsp not able to understand the ID for edit and delete. I was able to modify the edit link to make it recognize the bookId. But not sure how to modify the delete button code. Please advise.

<g:form url="[resource:bookInstance, action:'delete']" method="DELETE">
    <fieldset class="buttons">
        <g:link class="edit" action="edit" id="${bookInstance.bookId}" ><g:message code="default.button.edit.label" default="Edit" /></g:link></td>
        <g:actionSubmit class="delete" action="delete"  value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" />
    </fieldset>
</g:form>

Solution

  • You need to add a hidden field so that an id parameter is sent when the form is submitted, e.g.

    <g:form url="[resource:bookInstance, action:'delete']" method="DELETE">
    
        <g:hiddenField name="id" value="${bookInstance.bookId}" />
    
        <fieldset class="buttons">
            <g:link class="edit" action="edit" id="${bookInstance.bookId}" ><g:message code="default.button.edit.label" default="Edit" /></g:link></td>
            <g:actionSubmit class="delete" action="delete"  value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" />
        </fieldset>
    </g:form>