2013-03-02 58 views
0

使用simple_form_for,應該如何構建類似於顏色選擇器的東西?設想一個頁面,用戶可以通過選中複選框來選擇多種顏色進行處理。我想給出一個帶有顏色的小盒子和一個旁邊的複選框,以便用戶可以通過選中複選框來選擇顏色。類似這樣的東西簡單表單構建器複選框集合自定義html輸入

<input type="checkbox" name="colors[test]"><div style="background-color: red; width: 20px; height: 20px"></div> 
<input type="checkbox" name="colors[test]"><div style="background-color: green; width: 20px; height: 20px""></div> 
<input type="checkbox" name="colors[test]"><div style="background-color: blue; width: 20px; height: 20px""></div> 

回答

2

您可以將html標籤添加到集合中,併爲它們中的每一個定義類。然後相應地設計它們。我假設你有一個simple_form_for @color或類似的東西。

<%= f.input :test, :label => 'Choose a Color:', :collection =>{'<div class="red colorbox"></div>'.html_safe => 'red', '<div class="green colorbox"></div>'.html_safe => 'green', '<div class="blue colorbox"></div>'.html_safe => 'blue' }, :as => :check_boxes %> 

在你的樣式表:

/* The colorbox will be under a label with collection_check_boxes class.*/ 
.collection_check_boxes { 
    width: 30px; 
    height: 20px; 
} 

.colorbox { 
    width: 20px; 
    height: 20px; 
} 

.red {background: red;} 
.green {background: green;} 
.blue {background: blue;} 
+0

謝謝!這對於s​​imple_form來說似乎有點混亂。似乎並不簡單:)。現在,我也有要求,我必須將顏色分組。所以像「Colors.all.in_groups_of(4,false)」這樣的東西就是應該如何組織的。把它放在收藏中似乎沒有幫助,可以嗎?集合中的所有內容都被統一處理? – sat 2013-03-04 01:01:59

+0

你是什麼意思分組?你的意思是每行中有4個複選框還是別的? – 2013-03-04 04:55:10

+0

正確... 4行復選框。 – sat 2013-03-05 17:03:23