tags:

views:

40

answers:

1

I need to create a filter that is triggered on any save/update action in order to record the history of whats being done. I am having a problem getting id of the object that is being manipulated.

My controllers simplified:

class FooController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def save = {

        def fooInstance = new Foo(params)

        if (fooInstance.save()) {
           // params.id = fooInstance.id
            return redirect(action: "show", id: fooInstance.id)
        } else {
            return render(view: "create", model: [fooInstance: fooInstance])
        }
    }
}

My filter needs to get the id of the object that I put in return redirect but unless I uncomment params.id = fooInstance.id above my filter gets null for params.id, I must be missing something dealing with scopes of the variable:

 def filters = {

        history(controller: '*', action: 'save|update|delete|restore') {

            after = { 
                println params.id
            }
    }
}

I also tried passing model to after = expecting to get my fooInstance but I get null as well. Any ideas on whats wrong? Thanks.

+2  A: 

Look into this plugin http://grails.org/Hibernate+Events+Plugin or the grails audit log plugin. I think they will get you pretty far along.

It is a different approach then you mention in your question, but I think you might prefer it.

Aaron Saunders
Great plugins. While I am still trying to figure out why params.id not being passed, the Audit log plugin will do what I need better than filter would. Thanks again!
Micor