0

I'm working with Grails 2.1.0 and bumped with this 2 year old post: Is there a way to make functions accessible across controllers in Grails?

In there, Burt suggest to create a function that will be accessible to all controllers using Bootstrap. I have tried it with the findings that it works, however every time I modify the controller (add or delete whatever you may think) Grails gives me:

No signature of method: mycontroller.myfunctionfrombootstrap() is applicable for argument types: () values: []

Is this a "feature" of Grails 2.1.0? Am I missing something?

Upgrading Grails IS NOT an option.

In bootstrap I have:

for (controllerClass in grailsApplication.controllerClasses) {
    controllerClass.clazz.metaClass.myfunctionfrombootstrap= {
        return stuff; 
    }
}
Community
  • 1
  • 1
Alex Vargas
  • 406
  • 4
  • 15

1 Answers1

1

When you modify a controller in development mode Grails recompiles it and slots in the new class to replace the previous version. But bootstrap does not run again, so the new class will not have your extra function added to its metaclass.

Instead of using the metaclass you should consider putting reusable logic into a service or taglib.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183