How to Use Flash Messages in Rails: Simple Guide
In Rails, use the
flash hash to set temporary messages that last for one request cycle. Assign messages like flash[:notice] = 'Success!' in controllers, then display them in views with flash[:notice] or flash[:alert].Syntax
The flash is a special hash in Rails used to store messages that persist for one request. You set messages in the controller and read them in the view.
flash[:notice] = 'Message': Sets a success or info message.flash[:alert] = 'Message': Sets a warning or error message.- In the view, use
flash[:notice]orflash[:alert]to show the message.
ruby
class PostsController < ApplicationController def create @post = Post.new(post_params) if @post.save flash[:notice] = 'Post was successfully created.' redirect_to @post else flash[:alert] = 'Error creating post.' render :new end end private def post_params params.require(:post).permit(:title, :content) end end
Example
This example shows how to set a flash message in the controller after creating a post, then display it in the view layout.
ruby,erb
# app/controllers/posts_controller.rb class PostsController < ApplicationController def create @post = Post.new(post_params) if @post.save flash[:notice] = 'Post created successfully!' redirect_to @post else flash[:alert] = 'Failed to create post.' render :new end end private def post_params params.require(:post).permit(:title, :content) end end # app/views/layouts/application.html.erb <!DOCTYPE html> <html lang="en"> <head> <title>My Blog</title> </head> <body> <% if flash[:notice] %> <p style="color: green;"><%= flash[:notice] %></p> <% end %> <% if flash[:alert] %> <p style="color: red;"><%= flash[:alert] %></p> <% end %> <%= yield %> </body> </html>
Output
When a post is created successfully, the page shows a green message: "Post created successfully!". If creation fails, a red message: "Failed to create post." appears.
Common Pitfalls
- Setting flash messages but forgetting to redirect causes the message to persist longer than expected.
- Using
renderinstead ofredirect_tokeeps the same request, so flash messages may not clear as expected. - Not checking if
flash[:notice]orflash[:alert]exists before displaying can cause empty message areas.
ruby
class PostsController < ApplicationController def create @post = Post.new(post_params) if @post.save flash[:notice] = 'Post created!' redirect_to @post # Correct: flash will show once else flash[:alert] = 'Error!' render :new # Flash persists only for this render end end private def post_params params.require(:post).permit(:title, :content) end end
Quick Reference
| Flash Key | Purpose | Typical Use |
|---|---|---|
| notice | Informational or success messages | flash[:notice] = 'Saved successfully' |
| alert | Warnings or error messages | flash[:alert] = 'Something went wrong' |
| now | Shows message only for current request (no redirect) | flash.now[:alert] = 'Fix errors' |
Key Takeaways
Use
flash[:notice] and flash[:alert] in controllers to set messages for the next request.Display flash messages in views by checking if
flash[:notice] or flash[:alert] exists.Use
redirect_to after setting flash to show messages on the next page load.Use
flash.now when rendering the same page to show messages immediately without redirect.Always check for flash presence in views to avoid empty message areas.