2017-07-22 42 views
1

嘗試根據type根據不同的偏色設置不同的顏色;到目前爲止,有3種類型全部由整數1,23定義,我正在努力找到一種方法來使視圖中的部分以不同的背景顏色顯示。我試圖獲得12綠色3。我已經嘗試了很多東西,但似乎沒有任何工作。根據數據庫值將不同的背景顏色設置爲偏色

我試過讓before_action :set_colours,有if聲明部分,但沒有任何工作到目前爲止。

這裏是HTML,調用typeo.type

<div class="dropdown" style="background-color: <%= set_background(number) %>;"> 
    <button class="btn btn-block" type="button" id="dropdownMenuButton" 
    data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <%=o.id%>, <%=o.user_id%> 
    </button> 
    <div class="dropdown-menu btn-block" aria-labelledby="dropdownMenuButton"> 
    <li><a class="dropdown-item">Order Type</a></li> 
    <li><a class="dropdown-item"> Event type:</a> <p><%=o.event_type%></p></li> 
    <li><a class="dropdown-item"> Requirements/Ideas</a> <p><%=o.description%></p></li> 
    </div> 
</div> 

回答

1

你可以使用一個輔助函數將返回你需要基於類型的顏色,例如做:

def set_background_color(type) 
    case type 
    when 1 then "red" 
    when 2 then "blue" 
    when 3 then "green" 
    end 
end 

然後用它來設置您的div的背景顏色,使用style屬性,如下所示:

<div class="dropdown" style="background-color: <%= set_background_color(o.type) %>;"> 
    <!-- ... --> 
</div> 
+0

非常感謝! – Jack