Grails domain get(params.id) works in Controller.show() but not Contoller.edit()

john_solo picture john_solo · Apr 26, 2013 · Viewed 8.1k times · Source

So here's an interesting one. I've got a 2.1.1 Grails application with straigt-forward domains and controller with default scaffolding. My show() method works just find and retrieves the domain object with def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id) However, when I call the edit() method I get a java.lang.IllegalArgumentException - argument type mismatch on the exact same call def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.id) I've confirmed the params map is passing in the ID, and I've tried all variations of get(id), get(params), findByID(id), findByID(params) yadayadayada

Here's the form submit in the show.gsp that calls the edit method in the controller:

    <g:form>
        <fieldset class="buttons">
            <g:hiddenField name="id" value="${quarterEndAdjustmentInstance.id}" />
            <g:link class="edit" action="edit" id="${quarterEndAdjustmentInstance.id}"><g:message code="default.button.edit.label" default="Edit" /></g:link> 
            <%-- <g:actionSubmit class="edit" action="edit" value="${message(code: 'default.button.edit.label', default: 'Edit')}"  /> --%>
        </fieldset>
    </g:form>

Here are two closures from my controller. show() works fine, edit() throws the exception.

 def show() 
{   
    //params.each() { key, value -> println "${key} = ${value}" };
    def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id) //here are your inbound params
    if(!quarterEndAdjustmentInstance)
    {
        flash.message = "Quarter End Metric record not found with ${params}"
        redirect(action:"list", params: params)
    }
    else
    {
        quarterEndAdjustmentInstance.setFrName(mriUtils.getCompRecipient(quarterEndAdjustmentInstance.getCompPayeeID()))
        return [quarterEndAdjustmentInstance: quarterEndAdjustmentInstance]
    }
}

def edit() 
{
    def quarterEndAdjustmentInstance = QuarterEndAdjustment.get(params.id)

    if(!quarterEndAdjustmentInstance)
    {
        flash.message = "Quarter End M12 Adjustment not found with ${params}"
        redirect(action:"list", params:params)
    }
    else
    {
        quarterEndAdjustmentInstance.setFrName(mriUtils.getCompRecipient(quarterEndAdjustmentInstance.getCompPayeeID()))
        return [quarterEndAdjustmentInstance: quarterEndAdjustmentInstance]
    }   
}

Answer

user800014 picture user800014 · Apr 26, 2013

By default, Grails creates a Long attribute for your domain class.

If I'm not mistaken get() is the only method that will transform your String into the required Long. For the others you need to cast to long:

def quarterEndAdjustmentInstance = QuarterEndAdjustment.findByID(params.long('id'))