Why flash message won't disappear?

Ruby on-Rails

Ruby on-Rails Problem Overview


I am doing some exception handling in my controller, when there is an exception thrown in :create action, I will render to the :new action and show a flash message.

Everything works fine, I can see the flash message when exception caught, but when I redirect to(handly click) other page, the flash message still here. Then I redirect to another page(the second handly click), the message could disappear.

Anyone who knows what is the reason?

My controller code:

class MessagesController < ApplicationController
  rescue_from Exception, :with => :render_new
  
  def new
  end

  def create
  end

private
  def render_new
    flash[:alert] = t("uploading_error")
    render :action => :new
  end
end

My layout code (Haml):

%body
  #content
    - unless flash[:alert].blank?
      #alert= flash[:alert]

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Replace

flash[:alert] = t("uploading_error")

with

flash.now.alert = t("uploading_error")

and see if that is the result you expect?

flash[:alert] will stay around for the next page (hence it only disappears at the second redirect); but flash.now.alert will only display for the current page.

Solution 2 - Ruby on-Rails

Deciding between flash.now and regular flash is a pain in the ass and quite fragile in my experience. I use regular flash and then modify my partial which displays the flashes to delete the contents of each flash after the user has seen it. I think this is better because

a) you don't have to think about it

b) "has the user seen it?" (ie "has the flashes partial been rendered out?") is the best criterion for deciding whether or not to clear the flash, rather than any logic in your app.

My flash partial looks like this - i also use a bit of jquery just to highlight the flashes (ie make them flash yellow for a second):

<div id="flashes">
  
  <% if flash[:notice] %>
    <p id="flash_notice" class="messages notice"><%= flash[:notice] %></p>
    <%= javascript_tag "$('#flash_notice').effect('highlight',{},1000);" %>
  <% end %>
  
  <% if flash[:error] || flash[:errors] %>
    <p id="flash_errors" class="messages errors"><%= flash[:error] || flash[:errors] %></p>
    <%= javascript_tag "$('#flash_errors').effect('highlight',{},1000);" %>
  <% end %>
  
  <% flash[:error] = flash[:errors] = flash[:notice] = nil %>
</div>

Solution 3 - Ruby on-Rails

An alternative is to use flash.clear at the end of the partial like so:

<% if !flash.empty? %>
  <div class="flash-messages-container">
    <% flash.each do |name, msg| %>
      <% if msg.is_a?(String) && [:success, :info, :error, :warning].include?(name) %>
        <div class="flash-message" data-type="<%= name %>" >
          <%= msg %>
        </div>
      <% end %>
    <% end %>
  </div>
  <% flash.clear %>
<% end %>

Solution 4 - Ruby on-Rails

I also suggest to clear the flash inner hashes upon displaying. flash.clear will do the trick in a clean way :

      <% flash.each do |key, value| %>
       <div class="alert alert-<%= key %>">
        <%= value %>
       </div>
      <% end %>
      <% flash.clear %> #this line clears the object

http://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html#method-i-clear

Solution 5 - Ruby on-Rails

Even this does not work......certain types of exceptions like syntax errors...will prevent any type of cookie, flash or parameters from being transferred from controller to view. Your only option is to use a session key and then clear it after showing the error.

Try your solution with a syntax error...you should see that your message won't appear in the redirected page with anything else except with a session key.....

Solution 6 - Ruby on-Rails

Previously I had same problem,But now solved through this:
Try this in your code

<script type="text/javascript">
  $(document).ready(function(){
    setTimeout(function(){
    $('#flash').fadeOut();
    }, 2000);
  })
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJimmy HuangView Question on Stackoverflow
Solution 1 - Ruby on-RailsZabbaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsMax WilliamsView Answer on Stackoverflow
Solution 3 - Ruby on-RailsCoderDaveView Answer on Stackoverflow
Solution 4 - Ruby on-Railsthomas dhView Answer on Stackoverflow
Solution 5 - Ruby on-RailssurtepView Answer on Stackoverflow
Solution 6 - Ruby on-RailsNimishView Answer on Stackoverflow