Rails has a built-in helper for bouncing your client back to the referring page:
redirect :back
It depends, however, on a correctly-set HTTP_REFERER environment variable, which you won’t always have. Search engine crawlers, for example, generally don’t have these, and there are some oddball browsers out there. (I’ve learned this from inspecting server logs, and from Hoptoad.)
So here’s one that works more exhaustively:
def redirect_back
redirect_to :back rescue redirect_to root_path
end
Put this in application.rb and you can start using it instead of redirect :back. (You can change root_path to something of your own if you like.)
Footnote:
DHH used this approach a while back; he called it redirect_back_or_default at the time. Restful Authentication later kind of coopted that method name for its own purposes, so I’ve moved away from it in order to avoid any confusion…