Erase a flash [: success] a few seconds after being shown to the user?

2

I have a registration form using Rails, where after the user completes the registration, he is redirected to the login page along with a flash[:success] that shows a welcome message.

My question is if I had some form in Rails itself to make this flash[:success] disappear after x seconds?

Or is it better to do this using javascript?

Following code:

UsersController

def create
    @user = User.new(user_params)
    if @user.save
      flash[:success]= "Cadastro realizado com sucesso!"
      redirect_to sign_in_path
    else
      render action: :new
    end
  end
    
asked by anonymous 09.08.2015 / 02:22

1 answer

1

Ruby will not give you access to this type of control, you need to use Javascript, an example: HTML:

<div id="flash">Flash[:success]</div>

JavaScript:

//executa a função depois de 5 segundos
window.setTimeout( fade_flash, 5000 );
function fade_flash() {
    $("#flash").fadeOut(1000); //o fadeOut dura 1 segundo
}

JSFiddle

    
09.08.2015 / 05:33