bragboy
bragboy

Reputation: 35542

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!

Upvotes: 11

Views: 2116

Answers (1)

Shadwell
Shadwell

Reputation: 34774

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.

Upvotes: 29

Related Questions