我在我的rails应用程序中使用了devise,作为标准,它附带了notice和alert方法,用于呈现特定的操作(例如,用户登录)。
我也在使用物化CSS框架,它们提供了一个干净的“烤面包”式通知。这是使notice和alert与Toast协同工作的第一种方法。
<% if notice %>
<script type="text/javascript">
Materialize.toast('<%= notice %>', 4000)
</script>
<% end %>
<% if alert %>
<script type="text/javascript">
Materialize.toast('<%= alert %>', 4000)
</script>
<% end %>有人能提供一种更清洁/更干燥的解决方案吗?感觉有点烦躁。
发布于 2016-12-15 20:25:31
它不应该是“最讨厌的”方式,但还是有点DRYer:
<% flash.each do |message_type,message %> %> <%= javascript_tag“Materialize.toast(‘#{message},4000)”%> <% end %>
发布于 2016-03-23 10:19:24
我不认为我一定会认为这种技术“无趣”。在我的生产应用程序中,这对我来说很好:
<% flash.each do |type, message| %>
<% if type == "success" %>
<div class="alert alert-success alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<i class="icon-check"></i>
<p><%= message.html_safe %></p>
</div>
<% elsif type == "toast" %>
<script>
$(function() {
Materialize.toast("<%= message %>", 3000);
});
</script>
<% else %>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<i class="icon-notice"></i>
<%= message.html_safe %>
</div>
<% end %>
<% end %>只是我的意见,但我不认为有什么不对。
底线是,除了这样做之外,我不认为有什么办法可以让你的闪光灯触发js (但如果有人认为他们有更好的解决方案,我会全神贯注)。
发布于 2017-02-26 12:59:50
这是我的解决方案。主要代码只有两行。
<% unless flash.empty? %>
<script>
<% flash.each do |f| %>
<% type=f[0].to_s.gsub('alert', 'red').gsub('warning', 'orange').gsub('success', 'green') %>
Materialize.toast('<%= f[1] %>', 4000, '<%= type %>')
<% end %>
</script>
<% end %>https://stackoverflow.com/questions/35350592
复制相似问题