让Flash消息消失在Rails的同一页面中 [英] Let flash messages disappear in the same page in rails

查看:86
本文介绍了让Flash消息消失在Rails的同一页面中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让Flash消息在几秒钟后消失.因此,我使用以下代码:

I want to make the flash message disappear after a few seconds. So I use these codes:

$(function() {
  $('.alert').slideUp(1500);
});

.alert是我添​​加到Flash消息中的引导程序类.

.alert is the bootstrap class I add to the flash message.

我的应用仅通过omniauth实现了用户登录/注销功能. (使用facebook和google plus),最后列出了登录/注销代码.

And my app achieved user login/logout function only through omniauth. (with facebook and google plus) The login/logout codes listed in the end.

当我登录时,Flash消息按照我的想法消失了.但是当我注销时,闪光灯并没有消失.

When I log in, the flash message disappeared like what I thought. But when I log out, the flash didn't disappear.

但是,如果我使用这些代码,则此功能将正常运行. (但与我想要的略有不同)

But if I use these codes, this function will work correctly. (but slightly different than what I want)

$(function() {
  setInterval(function(){
    $('.alert').slideUp(500);
  }, 1000);
});

登录/退出代码:

class SessionsController < ApplicationController
  def create
    auth_data = request.env['omniauth.auth']
    auth = Authorization.find_by( provider: auth_data['provider'], uid: auth_data['uid'] ) 
    user = auth.nil? ? User.create_with_omniauth(auth_data) : auth.user
    session[:user_id] = user.id
    if !user.email
      redirect_to edit_user_path(user), :alert => "Please enter your email address."
    else
      redirect_to root_url, :notice => "Signed in!"
    end
  end

  def destroy
    reset_session
    redirect_to root_url, :notice => "Signed out!"
  end

  def new
  end

  def failure
    redirect_to root_url, :alert => "Authentication error: #{params[:message].humanize}"
  end
end

推荐答案

尝试一下

$(function() {
  setTimeout(function(){
    $('.alert').slideUp(500);
  }, 1000);
});

这篇关于让Flash消息消失在Rails的同一页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆