加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

RailsCasts18 Looping Through Flash 使用循环展现flash

发布时间:2020-12-15 07:00:40 所属栏目:百科 来源:网络整理
导读:有时layout文件中会展示几种flash messages;每种flash在展现前均会判断是否为nil,则均需要花费3行代码,如下: rubyhtml !-- (head snipped) -- body h1ASCIIcasts/h1 % unless flash[:notice].nil? % div id="notice"%= flash[:notice] %/div % end % % un

有时layout文件中会展示几种flash messages;每种flash在展现前均会判断是否为nil,则均需要花费3行代码,如下:

ruby

<html>
 <!-- (head snipped) -->
  <body>
    <h1>ASCIIcasts</h1>
    <% unless flash[:notice].nil? %>
      <div id="notice"><%= flash[:notice] %></div>
    <% end %>
    <% unless flash[:notice].nil? %>
      <div id="notice"><%= flash[:notice] %></div>
    <% end %>
    <%= yield %>
  </body>
</html>
使用循环可有效的减少代码量,如下:

<% flash.each do |key,msg| %>
  <%= content_tag :p,msg,:id => key %>
<% end %>

content_tag需要3个参数,第一个为element的名字,第二个为element的start、end标签中的内容,第三个参数为hash,规定element的属性。与之前的代码相比,现在的代码量是更小的,且灵活的。


原文:http://railscasts.com/episodes/18-looping-through-flash?view=asciicast

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读