Rails Flash messages for user feedback

5688
0

Flash messages provide one-time notifications across requests, perfect for confirming actions or showing errors. I use flash[:notice] for success messages and flash[:alert] for errors. Flash persists in session storage for one request, then auto-deletes. For AJAX requests, I return flash messages in JSON responses since redirects don't happen. The flash hash supports any keys—I add :warning and :success for semantic variety. Flash messages display in layouts, typically near the top of pages. For APIs serving React, I skip flash entirely and send messages in response bodies. Flash works beautifully for traditional request/response but needs adaptation for SPAs.