Imagine a user has just posted data to your web application and you want to re-display the current page with a message about their success or failure. This gets complicated.
If the data is valid and the user is expecting html, you want to issue a redirect so that refreshing doesn't cause them to re-post. You want to redirect to the referer, if it exists, and display a message. If they are not expecting html, you can simply return 200 OK.
If the data is invalid and the user is expecting html, you want to re-render the page they came from, with a visible error, so that they can re-post. To do this, you'd have to run the previous action, and make it aware of the error message. To decide which was the previous action, perhaps you included that as a hidden parameter in the form. If they are not expecting html, you can return an applicable 4xx client error.
I find myself doing this silly dance far too many times. So the questions are:
1) How would you abstract this whole process so that any form post can take advantage of it?
2) What's the most maintainable or least repetitive way to achieve this in your favorite web framework?
3) Is there anything you would change to this whole process that would make it simpler?
Idea 1: Never render on a post, always redirect. Stuff the error data in the session for a split second between requests, and then clear it, just like the success message. That way, valid and invalid posts can be handled the same way.
Idea 2: Don't do any normal HTTP posts. Only use ajax. Now you don't have to worry about rendering or redirecting at all. This would only be useful if you have an ajax-heavy application already.