2014-12-04 188 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