在 Rails 的管理后台中,经常要返回一些错误提示并显示在 redirect_to 或者 render 的页面中,但当我们点击新的链接,就想要这个错误提示消失掉,不停留在新的页面。

解决方案

redirect_to

controller.rb
# 第一种
redirect_to admin_shops_path, notice: '创建成功'
# 第二种
flash[:noite] = '创建成功'
redirect_to admin_shops_path
# 第三种
flash.discard[:noite] = '创建成功'
redirect_to admin_shops_path

render

controller.rb
flash.now[:noite] = '创建成功'
render :new

js

其实这种的话,可以搭配上面的一起使用,设置一个定时器自动消失。

// 清除 flash alert 消息
$(document).ready(function(){
setTimeout(function(){
$('.alert').fadeOut();
}, 5000);
});

参考资料