# Helper

**URL:** <https://www.ruby-forum.com/t/helper/163633>\
**Category:** Rails\
**Created:** [March 23, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/helper/163633 "2009-03-23T13:31:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 23, 2009, 1:31pm UTC](https://www.ruby-forum.com/t/helper/163633/1 "2009-03-23T13:31:40Z")

</div>

How can I replace this with helper?

\<% if flash[:error] %\>

\<%= flash[:error] %\>
  
\<% end %\>  
\<% if flash[:warning] %\>  

\<%= flash[:warning] %\>
  
\<% end %\>  
\<% if flash[:notice] %\>  

\<%= flash[:notice] %\>
  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 23, 2009, 2:09pm UTC](https://www.ruby-forum.com/t/helper/163633/2 "2009-03-23T14:09:32Z")

</div>

add one def in application\_helper.rb  
like :

def show\_flash(flash)  
html = “”  
if flash[:error]  
html += “

#{flash[:error]}
”  
end  
if flash[:warning]  
html + = “
#{flash[:warning]}
”  
end  
if flash[:notice]  
html += “
#{flash[:notice]}
”  
end  
end

then change in view.  
from  
\<% if flash[:error] %\>

\<%= flash[:error] %\>
\<% end %\> \<% if flash[:warning] %\> 
\<%= flash[:warning] %\>
\<% end %\> \<% if flash[:notice] %\> 
\<%= flash[:notice] %\>
\<% end %\>

To  
\<%= show\_flash(flash) %\>

I hope it will help u…

Thank you

On Mon, Mar 23, 2009 at 7:31 PM, Fresh M.  
\<removed\_email\_address@domain.invalid

> wrote:

> \<%= flash[:notice] %\>
> \<% end %\> -- Posted via http://www.ruby-forum.com/.
> 
> >

–  
Wu You Duan

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 23, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/helper/163633/3 "2009-03-23T15:54:50Z")

</div>

Or something like this:

def flash\_messages  
messages = []  
%w(notice warning error).each do |msg|  
messages \<\< content\_tag(:div, html\_escape(flash  
[msg.to\_sym]), :id =\> “flash-#{msg}”) unless flash[msg.to\_sym].blank?  
end  
messages  
end

\<%= flash\_messages %\>

---

<div class="post-metadata">

**Author:** ![giga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b9bd4f/32.png) [@giga](https://www.ruby-forum.com/u/giga)\
**Post date:** [March 23, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/helper/163633/4 "2009-03-23T16:27:16Z")

</div>

Another similar method…

def show\_flash  
[:notice, :warning, :message].collect do |key|  
content\_tag(:div, flash[key], :class =\> “flash\_#{key}”) unless  
flash[key].blank?  
end.join  
end
