Rails – Show Flash Message

The flash message is useful for notifying user. Rails 3 allows simple manipulation of the flash message.

1. Add the flash message in app/views/layouts/application.html.erb

...
<body>
...
  <% flash.each do |key, value| %>
    <%= content_tag(:div, value, :class => "flash #{key}") %>
  <% end %>
...
</body>
...

 

2. Try to add flash message when you want to redirect in the controller

...
redirect_to root_path, :notice => "You Flash Message!!"
...

 

3. You can also add the flash message in the controller in the following way

...
if @service.save
  flash[:success] = "Service created!"
  redirect_to services_path
else
  flash[:alert] = "Cannot create service!"
  render 'services/new'
end
...

 

Done =)

Reference: Ruby on Rails Tutorial: Learn Rails by Example – The flash

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s