0
votes

I'm trying to create my own 'edit' form in my grails application.

My g:select is currently populated with stuff from my database and looks like this:

<g:select name="nameList" from="${Card.list()}" value="${name} "  />

And then the value :

 <g:field name="amount" type="number" value="" required=""/>

My domain has only two variables - name and amount. I want to select the item from the dropdown box, type in the amount and just click 'update', my update method is a default one generated by grails so it requires ID and Version, how would I go about passing it through?

My update button ;

<g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" />

My domain code:

package cardstorage
class Card {
    String name;
    int amount;

    static constraints = {

        name(blank:false);
        amount(blank:false);
    }
    String toString(){
        return name;
    }
}

Thank you

1
I assume clicking Update doesn't work. What sort of error are you getting when you click the Update button? - grantmcconnaughey
I get a 'Card not found with id null' error. - ChrisA
Is your domain class using a custom ID generator? For example, generator: assigned? Could you copy and paste the domain class, please? - grantmcconnaughey
Modified my question. - ChrisA
Are you using Grails scaffolding for your view? Make sure you have a hidden field that contains the ID, otherwise your ID will be null when it tries to update because its not being passed to the Update controller action. For example, <g:hiddenField name="id" value="${cardInstance?.id}" /> This will be there if you are using scaffolding, but if not you may need to add it yourself. - grantmcconnaughey

1 Answers

0
votes

I have fixed it but I'm sure it is not a proper way to do so.

<g:form method="post" >

       <g:select name="card" from="${Card.list()}" optionValue ="name" optionKey="id" />

      <g:field name="amount" type="number" value="" required=""/>

        <fieldset class="buttons">
    <g:actionSubmit class="save" action="update" value="${message(code: 'default.button.update.label', default: 'Update')}" />
</fieldset>
</g:form>

thats my code for the g:select. In my Controller method, passing the value of 'card' to the 'Long id' would result in 'id' being 49 (null + 1 = 49?)

def update(Long id, Long version) {

    id = params.card;
    id = id- 48;
  ...  
}

Now I'm able to update my records, however I'm curious how should I have done this more properly.