views:

29

answers:

1

I got rid of the original UPDATE gsp Grails offers.

I put it in the first row of my list.gsp table and change all the values of the table to g:textfield so they can be edited without going to the save.gsp

But now I'm trying to make it work, and I can't.

I added a update button in the last column of the row, of every row.

When I change the values of the g:textfields and click the update button it tells me

Density #ID updated

but the values do not change.

I think I am doing something wrong with def update in the controller.

Here is the code:

def update = {
        log.info "Entering Action ${actionUri}"

        def densityInstance = Density.get(params.id)
        if (densityInstance) {

                if(params?.Rcommodity) { 
                    println "${params.Rcommodity}"

                }
            if (params.version) {
                def version = params.version.toLong()
                if (densityInstance.version > version) {

                    densityInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'density.label', default: 'Density')] as Object[], "Another user has updated this Density while you were editing")
                    render(view: "list", model: [densityInstance: densityInstance])
                    return
                }
            }
            densityInstance.properties = params
            if (!densityInstance.hasErrors() && densityInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'density.label', default: 'Density'), densityInstance.id])}"
                redirect(action: "list", id: densityInstance.id)
            }
            else {
                redirect(action: "list", id: densityInstance.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'density.label', default: 'Density'), params.id])}"
            redirect(action: "list")
        }
    }

The Rcommodity is the name of the textfields created, I put a println to see if the value was right, now I don't know how to make the value of the textfield be the one entered, it gives me the same value it had before but it gives me the message saying that it was updated. The controller is DensityController and the domain is density

Any help would be greatly appreciated. Thanks :D

+1  A: 

Looks from the flash message being printed as though the instance is being updated (though the "#ID" bit looks odd - have you replaced the actual id?).

It might be that

densityInstance.properties = params

is not actually be matching any instance properties, so none are actually being changed before the save. Are you sure you've named your gsp input fields to match the names of your Density class fields? Is Rcommodity a property of Density, for example?

Might help to add the form bit of your gsp page, as well as the Density domain class.

Martin Dow
commodity is a property of Density, I should add Rcommodity as well??
fgualda87
Sorry about the delay - have a look at the 'Data Binding' documentation (http://grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.1.6 Data Binding). Your parameters (set from the gsp form input names) will need to match the densityInstances properties - I can't tell what you've called them, but guessed from one of the lines you pasted above.
Martin Dow
Ok but let's say I have a field in the first line, where I create all of them, with the name `commodity` so that's declared in the domain. Then the created one is `Rcommodity` should I add it to the domain as well to update it?? Is that what you are trying to say?
fgualda87
I'm not clear what the question is or what you're trying to achieve with Rcommodity. Make sure you've read and understood the 'Data Binding' section of the documentation I posted above. If you need more help then just post the domain class and form from your GSP page, as it's hard to see what's going on without those.
Martin Dow