Skip to content

Instantly share code, notes, and snippets.

@BadAllOff
Forked from suryart/application.html.erb
Last active August 16, 2018 17:55
Show Gist options
  • Save BadAllOff/9cc60ed62815ef6570c4 to your computer and use it in GitHub Desktop.
Save BadAllOff/9cc60ed62815ef6570c4 to your computer and use it in GitHub Desktop.
Rails 4 flash messages WITH GLYPHICONS using Twitter Bootstrap(bootstrap-sass: https://github.com/thomas-mcdonald/bootstrap-sass). An improved version of https://gist.github.com/roberto/3344628
// layout file
<body>
<div class="container">
<%= flash_messages %>
<%= yield %>
</div><!-- /container -->
</body>
module ApplicationHelper
BOOTSTRAP_FLASH_MSG = {
success: 'alert-success',
error: 'alert-danger',
alert: 'alert-warning',
notice: 'alert-info'
}
def bootstrap_class_for(flash_type)
BOOTSTRAP_FLASH_MSG.fetch(flash_type.to_sym, flash_type.to_s)
end
def bootstrap_icon_for flash_type
{ success: "ok-circle", error: "remove-circle", alert: "warning-sign", notice: "exclamation-sign" }[flash_type.to_sym] || "question-sign"
end
def flash_messages(opts = {})
flash.each do |msg_type, message|
concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} fade in") do
concat content_tag(:button, 'x', class: "close", data: { dismiss: 'alert' })
concat content_tag(:i, nil, class: "glyphicon glyphicon-#{bootstrap_icon_for(msg_type)}")
concat ' '
concat message
end)
end
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment