018 循环显示flash信息

Displaying flash messages in the layout can be a pain at times. In this episode you will learn an easy way to display any kind of flash message by looping through the hash.
 
有时候在layout中显示flash消息是比较痛苦的一件事情。这节我们来学习一种简单的方法,通过hash循环来实现各种flash消息的显示。
 
情形:
#layout/application.rhtml
<%unless flash[:notice].nil?%>
    <div id="notice"><%= flash[:notice]%></div>
<%end%>
<%unless flash[:error].nil?%>
    <div id="error"><%= flash[:error]%></div>
<%end%>
 
这段代码看上去有些冗余。flash是一个hash,所以可以通过hash循环的方式将其显示出来:
 
<%flash.each do |key,msg|%>
    <%= content_tag :div, msg, :id=>key%>
<%end%>
------------
content_tag在railsbrain中的说明:
 
content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
Returns an HTML block tag of type name surrounding the content. Add HTML attributes by passing an attributes hash tooptions. Instead of passing the content as an argument, you can also use a block in which case, you pass your options as the second parameter. Set escape to false to disable attribute value escaping.

Options

The options hash is used with attributes with no value like (disabled and readonly), which you can give a value of true in theoptions hash. You can use symbols or strings for the attribute names.

Examples

  content_tag(:p, "Hello world!")
   # => <p>Hello world!</p>
  content_tag(:div, content_tag(:p, "Hello world!"), :class => "strong")
   # => <div class="strong"><p>Hello world!</p></div>
  content_tag("select", options, :multiple => true)
   # => <select multiple="multiple">...options...</select>
  <% content_tag :div, :class => "strong" do -%>
    Hello world!
  <% end -%>
   # => <div class="strong"><p>Hello world!</p></div>




本文转自 fsjoy1983 51CTO博客,原文链接:http://blog.51cto.com/fsjoy/131730,如需转载请自行联系原作者
上一篇:网络视频会议 多媒体协作的平台


下一篇:WPF Popup 控件导致被遮挡内容不刷新的原因