Delete a flash[:Success] a few seconds after being shown to the user?

Asked

Viewed 41 times

2

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

My doubt is whether there would be any way in the proper Rails to make that flash[:success] disappear after x seconds?

Or you’d better do it using javascript?

Follows 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

1 answer

1


The 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.