Forum: Ruby on Rails Helper

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ead43bcb0202410540a6a7cee5955d46?d=identicon&s=25 Fresh Mix (giga)
on 2009-03-23 13:31
How can I replace this with helper?

<% if flash[:error] %>
    <div class="error"><%= flash[:error] %></div>
<% end %>
<% if flash[:warning] %>
    <div class="warning"><%= flash[:warning] %></div>
<% end %>
<% if flash[:notice] %>
    <div class="notice"><%= flash[:notice] %></div>
<% end %>
7e35ecb8cb04c4c15cf0ad1a0edf113d?d=identicon&s=25 anton effendi (Guest)
on 2009-03-23 14:09
(Received via mailing list)
add one def in application_helper.rb
like :

 def show_flash(flash)
   html = ""
    if flash[:error]
      html += "<div class="error">#{flash[:error]}</div>"
    end
   if flash[:warning]
     html + = "<div class="warning">#{flash[:warning]}</div>"
   end
   if flash[:notice]
     html += "<div class="notice">#{flash[:notice]}</div>"
   end
 end

then change in view.
  from
 <% if flash[:error] %>
   <div class="error"><%= flash[:error] %></div>
<% end %>
<% if flash[:warning] %>
   <div class="warning"><%= flash[:warning] %></div>
<% end %>
<% if flash[:notice] %>
   <div class="notice"><%= flash[:notice] %></div>
<% end %>


To
<%= show_flash(flash) %>


I hope it will help u..

Thank you


On Mon, Mar 23, 2009 at 7:31 PM, Fresh Mix
<rails-mailing-list@andreas-s.net
> wrote:

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


--
Wu You Duan
4c438a80fc30661ad619ea177cf9cbd0?d=identicon&s=25 Freddy Andersen (Guest)
on 2009-03-23 15:54
(Received via mailing list)
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 %>
3ff9c2544554b13c6c7877e76684751b?d=identicon&s=25 Lee Smith (smitjel)
on 2009-03-23 16:27
(Received via mailing list)
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
This topic is locked and can not be replied to.