Posts Tagged: Flash


5
Mar 10

Outputting the flash to views and layouts

Here is a simple way of outputting the first message in the flash (because I didn’t want it to output both flash[:error] and flash[:notice] at the same time).

# in the controller
flash[:notice] = "Thanks for signing up!"

.. and…

# in the view
<% flash.detect do |type, text| %>
	<div id="flash" class="<%= type.to_s %>">
		<%= text %>
	</div>
<% end %>

The detect method will return the first key/value pair of the flash where the block doesn’t return false. However, that’s not what we’re using it for – we’re using it to make sure we only get the first element.

Hope this is useful.


Fork me on GitHub