问题
I currently have standard flash messages with the devise gem for success/failure etc. I have added the option to manually close the message with some bootstrap functionality via a close class. A small snippet is shown below.
{
<a class="close" data-dismiss="alert">×</a>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
}
I was would like to have an option to create a timeout period where the alert message would close on its one after 5 seconds. Not sure if there is a simple way to do this in Rails.
Thanks
回答1:
If you've jQuery loaded in the same page, this will work for you
<div id="flash">
<a class="close" data-dismiss="alert">×</a>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<script type="text/javascript">
$(document).ready(function(){
setTimeout(function(){
$('#flash').remove();
}, 5000);
})
</script>
回答2:
- Ensure that your div has an
alert
class. - Add the following code in your
application.js
or other.js
file:
$(document).on('turbolinks:load', function() {
setTimeout(function() {
$('.alert').fadeOut();
}, 3000);
})
- Watch it go away.
来源:https://stackoverflow.com/questions/15577555/how-to-timeout-flash-messages-in-rails