2014-12-04 190 views
1

我正在使用Bootstrap進入我的頁面,並且遇到了Flash錯誤的問題。我需要所有閃光燈獲得Bootstrap風格,所以我製作了一個輔助模塊來做到這一點。 Flash通知適用於風格,但是當我強制出現錯誤時,它只是在沒有風格的情況下出現「級別不能爲空」。 Tnis是我的代碼:將Bootstrap樣式應用於Flash錯誤

_flash_messages.html.erb

<% flash.each do |type, message| %> 
     <div class="alert <%= bootstrap_class_for(type) %> fade in"> 
     <button class="close" data-dismiss="alert">×</button> 
     <%= message %> 
     </div> 
    <% end %> 

application_helper.rb

module ApplicationHelper 

def bootstrap_class_for flash_type 
if flash_type == "success" 
    "alert-success" 
elsif flash_type == "error" 
    "alert-error" 
elsif flash_type == "alert" 
    "alert-block" 
elsif flash_type == "notice" 
    "alert-info" 
else 
    flash_type.to_s 
end 
end 

_form.html.erb

<% if @control.errors.any? %> 
<div class="alert alert-error fade in"> 

<% @control.errors.full_messages.each do |error_message| %> 
    <p><%= error_message %></p> 
</div> 
<% end %> 

<% end %> 

回答

1

我用這個幫手:

def alert_class_for(flash_type) 
    { 
     :success => 'alert-success', 
     :error => 'alert-danger', 
     :alert => 'alert-warning', 
     :notice => 'alert-info' 
    } [flash_type.to_sym] || flash_type.to_s 
    end 

在我看來,(我用修身,讓你要我unslim告訴我一聲):

- flash.each do |type, message| 
    .alert.alert-dismissible.fade.in class=(alert_class_for(type)) 
    = message 
+0

好,不過我使用的軌道,我不會寫代碼,而不大成。我想知道,如果我需要我在_form.html.erb中寫的代碼,或者如果它足夠使用flash.each,那麼...... – 2014-12-04 09:13:05

+0

:)我也使用Rails。 Slim是一個Rails'gem。它允許編寫更少的html。 (http://slim-lang.com)。 – 2014-12-04 20:16:46

+0

謝謝你,我會看看它 – 2014-12-04 22:56:27