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 to options. 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 the options  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,如需转载请自行联系原作者
目录
相关文章
从键盘上输入10个整数,写入到一个文件number.txt中,然后从此文件读出数据,显示在屏幕上。
从键盘上输入10个整数,写入到一个文件number.txt中,然后从此文件读出数据,显示在屏幕上。
267 0
|
人工智能 自然语言处理 前端开发
Network可以显示后台返回的数据,但是打印出来时是undefind
Network可以显示后台返回的数据,但是打印出来时是undefind
115 0