2010-01-01 1 views
2

모델에서 색상 목록을 가져 와서 색상 선택 목록을보기에 넣고 싶습니다. 옵션의 색 코드 값이 있습니다. 그 값과 같은 옵션의 배경색을 변경하고 싶습니다. 아래에 두 가지 방법을 시도했지만 전체 선택 목록의 색상이 변경되지만 개별 색상의 개별 옵션은 변경되지 않습니다.ruby ​​on rails : 선택 목록에서 옵션의 BG 색상을 변경하는 방법

<p> 
    <b>Background color</b><br />  
    collection_select(:setting, :bg_color, @colors, :code, :name, 
    options ={:prompt => "-Select a color"}, html_options = {:style => "background-color: #3f3"}) 
    </p> 

    <p> 
    <b>New Background color</b><br /> 
select_tag("setting[bg_color]", "<option>-Select a color</option>" + 
    options_from_collection_for_select(@colors, :code, :name, html_options = {:style => "background-color: #3f3"})) 
    </p> 

내가이 비슷한 생성하려면이 방법

<option value='color_code' style='background-color: color_code' >color_name</option> 

을, 옵션의 색상은 사용자에게 볼 수 있습니다.

어쨌든 옵션을 사용자 정의 할 수 있습니까?

미리 감사드립니다.

답변

4

레일 헬퍼는 이와 같은 html_options를 지원하지 않습니다. 그래도 자신 만의 도우미 방법을 만들 수 있습니다.

def options_with_colors(colors) 
    colors.collect do |color, code| 
    "<option value='#{code}' style='background-color:#{code};'>#{color}</option> " 
    end.join 
end 

전화와 같은 :

@colors = ["Red" => "#f00", "Blue" => "blue"] 
select_tag("setting[bg_color]", options_with_colors(@colors)) 
+0

감사합니다! 정말 효과가있었습니다. – AustinPower