Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I dismiss a Rails flash message after some duration?

I would like to set the number of seconds a flash notice is shown to the user, before it is automatically dismissed.

like image 379
Robert Kajic Avatar asked Jan 30 '14 20:01

Robert Kajic


People also ask

What is a flash error message?

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 flash messages work Rails?

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.


2 Answers

You can use some simple JavaScript on your page (using jQuery in this example):

$('document').ready(function() {
  setTimeout(function() {
    $('#flash').slideUp();
  }, 3000);
});

Assuming the id of the HTML element holding your flash message is #flash, this will slide it up and hide it after 3000 milliseconds (3 seconds).

like image 131
Louis Simoneau Avatar answered Oct 14 '22 01:10

Louis Simoneau


Just combining what @LouisSimoneau and @rlecaro2 already mentioned – I currently use:

function clearNotice(){
  $(".notice").animate({opacity:'0'}, 1500);
}

Note that if your using rails 4 with turbolinks, you'll need to call it from a ready function:

$(document).ready(ready);
$(document).on('page:load', ready);

var ready = function() {    
   setTimeout(clearNotice, 1000);  //Flash fade
};
like image 31
msanteler Avatar answered Oct 14 '22 03:10

msanteler