Skip to content

Instantly share code, notes, and snippets.

@yildizib
Last active January 22, 2017 13:07
Show Gist options
  • Save yildizib/11eab3f91a503a05360c78750fab929c to your computer and use it in GitHub Desktop.
Save yildizib/11eab3f91a503a05360c78750fab929c to your computer and use it in GitHub Desktop.
rails bootstrap flash messages helper
module ApplicationHelper
def bootstrap_class_for flash_type
{success: "alert-success", error: "alert-danger", alert: "alert-warning", notice: "alert-info"}[flash_type.to_sym] || flash_type.to_s
end
# flash messages display
def flash_messages(opts = {})
flash.each do |msg_type, message|
concat(content_tag(:div, message, class: "alert #{bootstrap_class_for(msg_type)} alert-dismissible", role: 'alert') do
concat(content_tag(:button, class: 'close', data: {dismiss: 'alert'}) do
concat content_tag(:span, '×'.html_safe, 'aria-hidden' => true)
concat content_tag(:span, 'Close', class: 'sr-only')
end)
concat message
end)
end
nil
end
# model errors display
def form_errors_messages(entity, opts = { flash: false })
if entity.errors.any?
concat(content_tag(:div, nil, class: "alert #{bootstrap_class_for(:error)} alert-dismissible", role: 'alert') do
concat(content_tag(:button, class: 'close', data: {dismiss: 'alert'}) do
concat content_tag(:span, '×'.html_safe, 'aria-hidden' => true)
concat content_tag(:span, 'Close', class: 'sr-only')
end)
concat(
content_tag(:ul) do
entity.errors.full_messages.each do |error|
concat(
content_tag(:li) do
error
end
)
end
end
)
end)
elsif opts[:flash] and flash.any?
flash_messages
flash.clear
end
nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment