Redirecting to the proper page after actions

A couple days ago, a client I currently work for found a little annoyance with cakePHP default’s behaviour of redirecting to the index action after a delete or edit action, thus ignoring the page you were on.
Indeed, redirecting to $this->referer wouldn’t work in this case because the data is posted to the same action, therefore $this->referer = $this->here. The solution is quite simple, add this code to your action:

if ($this->referer != $this->here)
$this->Session->write('referer', $this->referer);

Then, if your save method is successful, you can safely redirect with the Session variable that’s just been set :

$this->redirect($this->Session->read('referer'));

It’s pretty simple, but I guess it might come in handy. Thanks to TommyO from the irc channel for this hint!