how to use method in another controller

http://stuff4j.blogspot.ca/2011/04/calling-controller-method-from-another.html

Supposing I have a controller for books called BookController and a controller for authors called AuthorController and I want to remove a certain book when changing some author data:

In AuthorController :
// if some logic condition is met
BookController bookController = new BookController()
bookController.params.id = …
bookController.delete()
Normally the AuthorController.delete method will redirect you to some view and you normally don’t want that – the redirection should be done by BookController. In this case you can send a parameter to the controller and use it there to skip the redirection:
In AuthorController :
BookController bookController = new BookController()
bookController.params.id = …
bookController.params.doNotRedirect = ‘true’
bookController.delete()
In BookController.delete:
if(!’true’.equals(params.doNotRedirect)){
flash.message = …
redirect(action: “show”, id: book.id)
}
Advertisements
This entry was posted in Grails. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s