Last active
August 29, 2015 14:05
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%- # 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">×</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 %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.