2016-02-11 15 views
4

我在我的rails應用程序中使用設備,並作爲標準配備noticealert在特定操作(例如用戶登錄)上呈現的方法。乾淨的方法來設計通知/警報實現烤麪包

我也使用Materialize CSS框架,他們提供了一個很好的清潔'Toast' style notification。這是使noticealert與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 %> 

任何人都可以提供更清潔/更乾的解決方案嗎?感覺有點哈克。

回答

6

它肥大是不是「hackyest」的方式,但還是有點乾燥機:

<% flash.each do |message_type, message| %> 
    <%= javascript_tag "Materialize.toast('#{message}', 4000)" %> 
<% end %> 
3

我不認爲我會認爲這種技術'哈克'。這很適合我在我的生產應用程序:

<% 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">&times;</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">&times;</span> 
     </button> 
     <i class="icon-notice"></i> 
     <%= message.html_safe %> 
    </div> 
    <% end %> 
<% end %> 

只是我認爲在這裏,但我看不出有什麼不妥。底線是我認爲沒有一種方法可以讓你的閃光燈觸發js,而不是這樣做(但是如果有人認爲他們有更好的解決方案,我全都是耳朵)。

+0

這可能會更好。警告html非常相似。真的應該抽象成一個div。 – gregblass

+0

在:Materialize.toast('<%= message%>')中使用雙引號......否則,如果郵件中有撇號,郵件將不會顯示 – jpwynn

+0

Good all @jpwynn。已更新。 – gregblass

0

這裏是我的解決方案。主要的代碼只有兩行。

<% 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 %>