显示快闪消息的最佳实践方法 [英] Best practice method of displaying flash messages
问题描述
我想知道显示 Flash 消息的最佳做法是什么.我见过的两种主要方法是使用类似这种脚手架生成的代码
<%= notice %>
或将这样的代码放在您的应用程序标题中.
<% if !flash.empty?%><div id="闪存"><% flash.keys.each do |k|%><div class="<%= k %>"><%=闪光[k]%>
<%结束%>
<%结束%>
在我看来,第一种方法增加了更多的灵活性,而后者提高了代码的可读性并消除了冗余.有没有大多数 Rails 开发人员更喜欢的方法?作为一个附带问题,脚手架如何实施通知?它只是访问闪存哈希的助手吗?当您可以直接使用闪存哈希时,为什么还要麻烦使用助手?谢谢
我是这样做的:
<% flash.each do |key, value|%><%= content_tag :div, value, class: "flash #{key}" %><%结束%>
I'm wondering what's the best practice for displaying flash messages. The two main ways I've seen are using something like this scaffold generated code
<p id="notice"><%= notice %></p>
or placing code like this in your application header.
<% if !flash.empty? %>
<div id="flash">
<% flash.keys.each do |k| %>
<div class="<%= k %>">
<%= flash[k] %>
</div>
<% end %>
</div>
<% end %>
It appears to me that the first method adds more flexibility while the latter improves code readability and eliminates redundancy. Is there a method most rails developers prefer? As a side question how does scaffolding implement notice? Is it just a helper that accesses the flash hash? Why go through the trouble of using the helper when you can directly use the flash hash? Thanks
I'm doing it this way:
<% flash.each do |key, value| %>
<%= content_tag :div, value, class: "flash #{key}" %>
<% end %>
这篇关于显示快闪消息的最佳实践方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!