Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flash messages in Rails getting carried over to the next page

I am displaying error and notice messages in my application with a helper method as shown below.

  def display_flash_content
    [:error, :notice].map do |key|
      collection = flash[key].blank? ? [] : (flash[key].respond_to?(:map) ? flash[key] : [flash[key]])
      collection.map {|item| content_tag(:div, item, :class => key.to_s) }
    end
  end

and my layout has this

  <%= display_flash_content %>

I need to display these messages when I do some operation and then redirect to a specific page (this is working fine). But my problem is this flash message persists between pages. It's coming twice between pages where it needs to be cleared immediately once it's displayed to the user.

How to handle this scenario. Please help me!

like image 207
bragboy Avatar asked Aug 11 '10 15:08

bragboy


People also ask

How does Flash work in Rails?

A flash message is a way to communicate information with the users of your Rails application so they can know what happens as a result of their actions. Example messages: “Password changed correctly” (confirmation) “User not found” (error)

How do I use flash in Ruby on Rails?

In order to implement flash in your own apps, there's a specific set of steps that must be taken. You first call and set flash in your action controller. You must tell flash precisely what you want it to persist forward. Redirect your action controller to the full-page reload of your choice.

What is a flash notice?

Flash messages are notifications and alerts that pop up in the interface of an application in order to communicate with the user and ease of interaction. Applications often apply flash messages to tell the user if the login was correct or to confirm the success of the action triggered by clicking a button.


1 Answers

The way you are displaying the flash messages is fine. I think the problem is how you are setting them. If you are setting flash messages and not redirecting you can assign to flash.now[:notice] instead of flash[:notice], for example, and your message will not hang around after the redirect.

like image 175
Shadwell Avatar answered Sep 22 '22 05:09

Shadwell