Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

auto hide the flash messages in rails

I need to auto fade the flash messages in ruby on rails.

My message code is:

<%= simple_form_for(@dashboard_user) do |f| %>
<% if @dashboard_user.errors.any? %>
<ul class="alert alert-danger">
    <% for message_error in @dashboard_user.errors.full_messages %>
    <li>
        <%= message_error %>
    </li>
    <% end %>
</ul>
<% end %>

How i auto fade these messages?

like image 928
Raj Avatar asked Nov 06 '14 06:11

Raj


2 Answers

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);
like image 181
Shatabdi Mohapatra Avatar answered Sep 18 '22 10:09

Shatabdi Mohapatra


This one will work with Turbolinks-3.

$(document).on('page:change', function(){
$(".alert").delay(2000).slideUp(500, function(){
      $(".alert").alert('close');
  });
});

Turbolinks 5:

$(document).on('turbolinks:load', function(){
    $(".alert").delay(2000).slideUp(500, function(){
          $(".alert").alert('close');
      });
    });
like image 26
Abhinay Avatar answered Sep 19 '22 10:09

Abhinay