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

Rails宝典之第十八式: 循环flash

发布时间:2020-12-15 06:29:49 所属栏目:百科 来源:网络整理
导读:我们在application.rhtml(global layout)里经常需要写各种flash的显示:? Java代码?? %?unless?flash[:notice].nil??%?? ??div?id= "notice" %=?flash[:notice]?%/div?? %?end?%?? ?? %?unless?flash[:error].nil??%?? ??div?id= "error" %=?flash[:error]?%/
我们在application.rhtml(global layout)里经常需要写各种flash的显示:?
Java代码??

收藏代码

  1. <%?unless?flash[:notice].nil??%>??
  2. ??<div?id="notice"><%=?flash[:notice]?%></div>??
  3. <%?end?%>??
  4. ??
  5. <%?unless?flash[:error].nil??%>??
  6. ??<div?id="error"><%=?flash[:error]?%></div>??
  7. <%?end?%>??

我们可以循环来输出flash:?
<%?flash.each?do?|key,?msg|?%>??
  • ??<%=?content_tag?:div,?msg,?:id?=>?key?%>??
  • <%?end?%>??

  • 这样写倒是节约了代码,但是可能flash消息的顺序不是很好,我们可以这样写:?
    <%-?[:error,?:warning,?:notice,?:message].each?do?|key|?-%>??
  • if?flash[key]?%>??
  • <%-?end?-%>??
  • (编辑:李大同)

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

      推荐文章
        热点阅读