Skip to content

Instantly share code, notes, and snippets.

@AhmedNadar
Created November 22, 2013 21:33
Show Gist options
  • Save AhmedNadar/7607251 to your computer and use it in GitHub Desktop.
Save AhmedNadar/7607251 to your computer and use it in GitHub Desktop.
Using rails flash messages with Twitter Bootstrap. 1- Add <%= flash_messages %> to "application.html.erb" 2- Add 'flash_messages' method to "application_helper.rb"
<body>
<div class="container">
<%= render "layouts/header" %>
<%= flash_messages %>
<%= yield %>
<%= render "layouts/footer" %>
</div
</body>
module ApplicationHelper
def bootstrap_class_for flash_type
{ success: "alert-success", error: "alert-error", alert: "alert-warning", notice: "alert-info" }[flash_type] || flash_type.to_s
end
def flash_messages(opts = {})
capture do
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 message
end)
end
nil
end
end
# capture...do block, captures the content so it isn’t appended straight to the page.
# Instead, it’s built up as a separate string.
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment