Skip to content

Instantly share code, notes, and snippets.

@brianburridge
Last active August 29, 2015 14:05
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 brianburridge/0184a6fa2f1095e5e01c to your computer and use it in GitHub Desktop.
Save brianburridge/0184a6fa2f1095e5e01c to your computer and use it in GitHub Desktop.
How to pull specific flash messages for display in different parts of the page instead of grouping all flashes together in one place.
<%- # Show all flash messages in one place -%>
<% if flash.present? %>
<div id="myModal" class="reveal-modal">
<%- flash.each do |key, value| -%>
<div class="<%= key %>_flash"><%= raw value %></div>
<%- end -%>
<a class="close-reveal-modal">&#215;</a>
</div>
<% end %>
<%- # Show only success flash messages in this location -%>
<% if flash.present? && flash.has_key?(:success) %>
<div class="success_flash"><%= raw flash[:success] %></div>
<% end %>
@brianburridge
Copy link
Author

The Rails “flash” is just a Hash object set into a variable called ‘flash’. The .each iterates through each element of the Hash pulling out both the key (:success, :error, :notice, etc) and the value (line 3).
But instead of iterating, you can check for any specific key and then pull the value for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment