How to Timeout Flash Messages in Rails

How to timeout flash messages in rails

If you've jQuery loaded in the same page, this will work for you

<div id="flash">
<a class="close" data-dismiss="alert">×</a>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>

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

auto hide the flash messages in rails

This should work for you. You can specify the time span within the brackets. Add this to your Javascript. this is common for all:

$(".alert" ).fadeOut(3000);

For alert success:

 $(".alert-success" ).fadeOut(3000);

For alert danger:

$(".alert-danger" ).fadeOut(3000);

Message true flashes on main page after session timeout

Try outputting the type of each flash – maybe Devise or something else is using the flash for other things than just messages. Read more about using the flash for other things than messages.

I wouldn't loop over all flash keys to render them – I'd explicitly render only the ones that are dedicated to messages (usually :notice and :alert).

Devise 3.5.x on Rails 4.2.x timeout prints true underneath timeout flash message

I know this is a late reply, but this is what I did.

flash.each do |msg_type, message|
unless msg_type.to_s == 'timedout'
<!-- Carry on printing messages -->

Duplicate Devise flash messages

This piece of code renders the flash message:

<% if notice %><div class="alert alert-info"><%= notice %></div><% end %>
<% if alert %><div class="alert alert-warning"><%= alert %></div><% end %>

This piece of code also renders the (duplicate) flash message:

<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>

That is why you are seeing it twice. You should pick one and delete the other. I recommend keeping the second code segment.

Rails 4: Flash message persists for the next page view

Use flash.now instead of flash.

The flash variable is intended to be used before a redirect, and it persists on the resulting page for one request. This means that if we do not redirect, and instead simply render a page, the flash message will persist for two requests: it appears on the rendered page but is still waiting for a redirect (i.e., a second request), and thus the message will appear again if you click a link.

To avoid this weird behavior, when rendering rather than redirecting we use flash.now instead of flash.

The flash.now object is used for displaying flash messages on a rendered page. As per my assumption, if you ever find a random flash message where you do not expect it, you can resolve it by replacing flash with flash.now.



Related Topics



Leave a reply



Submit