在Rails中显示Flash通知的最佳方法 [英] Best way to display flash notices in Rails
问题描述
我在Rails和Devise中使用通知并想知道显示它们的最佳方式是什么?
I'm using notices in Rails and Devise and wondered what's the best way to display them?
在我的应用程序中我也有这样的用户保存时发布,它使用Ajax保存它,我使用jQuery显示通知。
In my application I also have it so when a user saves a post, it saves it using Ajax and I use jQuery to display a notice.
对于我使用的所有通知:
For all the notices I use:
<% flash.each do |key,msg| %>
<div class="message" id="<%= key %>">
<div class="message-inner">
<%= content_tag :div, msg %>
<a class="dismiss">x</a>
</div>
</div>
<% end %>
该代码为:
$("#notice").html("Saved.");
我希望它是一个闪光通知,以便它从顶部向下滑动几秒钟,它再次滑回。
I'd like it to be a flash notice so that it slides down from the top and after a few seconds, it slides back up again.
由于我在保存帖子而不使用Rails通知时使用jQuery通知,这意味着我必须拥有a < div id =notice>< / div>
以及 flash.each
in application.html.erb文件。
Since I'm using jQuery for the notice when saving a post and not using a Rails notice, it means I have to have a <div id="notice"></div>
as well as the flash.each
in the application.html.erb file.
我可以将它们全部加在一起所以我只使用一个通知并让它从顶部向下滑动然后滑动在几秒钟之后就离开了?
Can I add it all together so I'm only using one notice and have it so it slides down from the top and then slides out of the way after a few seconds?
推荐答案
我会默认隐藏通知。然后拨打
I would hide the notice by default. Then call
$('#notice').html("Saved.").slideDown(500).delay(3000).slideUp(500)
你可以调整jQuery方法中的数字但是你想要时间动画。
You can adjust the numbers in the jQuery methods to however you want to time the animations.
这篇关于在Rails中显示Flash通知的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!