Skip to content

Instantly share code, notes, and snippets.

@sergey-alekseev
Created March 9, 2013 23:09
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 sergey-alekseev/5126200 to your computer and use it in GitHub Desktop.
Save sergey-alekseev/5126200 to your computer and use it in GitHub Desktop.
Rails pretty flash messages with Twitter Bootstrap
# See http://twitter.github.com/bootstrap/components.html#alerts
#
# USAGE:
# flash.now[:info] = { title: 'New flash messages! ', body: 'Just add :info, :success, :warning or :error.' }
# redirect_to root_path, warning: 'You should use :success and :error instead of :notice and :alert'
#
# in application_controller.rb
def pretty_flash(name, msg)
msg_content = if msg.is_a?(String)
msg
elsif msg.is_a?(Hash)
content_tag(:strong) { msg[:title] } + msg[:body]
else
nil
end
# HACK: override devise :notice and :alert messages
name = 'success' if name.to_sym == :notice
name = 'error' if name.to_sym == :alert
if msg_content
content_tag(:div, class: "alert alert-#{name}") do
content_tag(:button, type: 'button', class: 'close', data: { dismiss: 'alert' } ) { '×'.html_safe } +
msg_content
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment