I’m really used to putting error messages in the flash Hash and displaying that to the user in a big error box. Until recently my usual way of coding up the workflow was to have a page submitting to a specific action, e.g.
class PasswordController < ApplicationController def password end def password_submit ... validate here ... end end
Here the user’s password form is shown on the password action, but the form action points to password_submit. The password_submit does the validation and sends the user back to password. Here password_submit was putting both success and error messages into the flash Hash. According to the Rails FlashHash documentat this is correct. Because I’m doing redirects from the password_submit to another action (“password”) the error and success messages get cleared out after being displayed.
However, in a more recent project I decided to do the validation right inside the password action itself, more like:
class PasswordController < ApplicationController def password if request.method == :post then ... validate here ... end end end
What started happening is if the user makes and error and then successfully changes their password, the flash error message AND the success kept showing up! I scratched my head for a while and did some Googling. Turns out that there is a big difference between flash and flash.now. Because the FlashHash is only cleared out on the next action the error message was on the “next” action and the success was on the “current” action—it is possible to have both in there.
By changing things to flash.now everything is working fine.