Skip to content

Instantly share code, notes, and snippets.

@lmmendes
Created March 23, 2011 17:22
Show Gist options
  • Save lmmendes/883529 to your computer and use it in GitHub Desktop.
Save lmmendes/883529 to your computer and use it in GitHub Desktop.
Rails flash messages helper
def flash_messages(options={})
flash_names = [:success, :warning, :error, :information, :tip]
html = ''
flash_names.each do |flash_type|
html += flash_message_for( flash[flash_type], options ) if flash[flash_type]
end
raw html
end
def flash_message_for(message, options={})
options = {
:type => :tip,
:closeable => true,
:timeout => 2500
}.merge( options );
id = 'flash_' + rand(Time.now.to_i).to_s
html = "<div id='#{ id }' class='message #{ options[:closeable] ? 'closeable' : '' } #{ options[:type].to_s }'>"
html += "<p>#{ message }</p>"
html += "<div class='close'>x</div>" if options[:closeable]
html += "</div>"
if options[:closeable]
html += "<script type='text/javascript'>"
html += "setTimeout( function(){ $('##{ id }').fadeOut(); }, #{ options[:timeout] } )"
html += "</script>"
end
raw html
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment