Skip to content

Instantly share code, notes, and snippets.

@TechFounder
Created December 17, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TechFounder/5bfd306b26ccd4afcf95403cd8b0b75c to your computer and use it in GitHub Desktop.
Save TechFounder/5bfd306b26ccd4afcf95403cd8b0b75c to your computer and use it in GitHub Desktop.
Rails flash message using bootstrap styling - underneath navbar, full width
<%= render 'layouts/navbar' %>
# Put this flash message block between the navbar and the yield
<% flash.each do |name, msg| %>
<% unless flash_blacklist(name) %>
<%= content_tag(:div, class: flash_class(name)) do %>
<%= msg %>
<button type="button" class="close" data-dismiss="alert">×</button>
<% end %>
<% end %>
<% end %>
<%= yield %>
# This will assign the correct bootstrap colors to the messages
def flash_class(name)
case name
when 'notice' then 'alert alert-info alert-dismissable fade show'
when 'success' then 'alert alert-success alert-dismissable fade show'
when 'error' then 'alert alert-danger alert-dismissable fade show'
when 'alert' then 'alert alert-danger alert-dismissable fade show'
else
'alert alert-primary alert-dismissable fade show'
end
end
# Exlude these messages from appearing
def flash_blacklist(name)
%w(timedout).include?(name)
end
# Flash message timer & slider
$(document).on 'turbolinks:load', ->
$('.alert').delay(4000).slideUp 500, ->
$('.alert').alert 'close'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment