如何自定义select_tag?

时间:2012-12-01 17:56:30

标签: ruby-on-rails-3 haml html-select

我想在Rails应用程序的select_tag中设置背景颜色

我目前的haml代码

= select_tag :color_id, options_from_collection_for_select(@colors, "id", "name")

产生

<label for="car_color_id">Choose color!</label>
    <select id="color_id" name="color_id">
      <option value="1">White</option>
      <option value="2">Red</option>
      <option value="3">Green</option>
   </select>

我想设置

<label for="car_color_id">Choose color!</label>
    <select id="color_id" name="color_id">
      <option value="1"><span style="background-color: #ffffff;">White</span></option>
      <option value="2"><span style="background-color: #ff0000;">Red</span></option>
      <option value="3"><span style="background-color: #00ff00;">Green</span></option>
   </select>

我的数据库中的十六进制标签

1 个答案:

答案 0 :(得分:0)

使用此选项按照建议生成输出:

= select_tag :color_id, options_from_collection_for_select('<span style="background-color: {#@colors};">{#@colors}</span>', "id", "name")

OR

= select_tag :color_id, options_from_collection_for_select('<span style="background-color: '+@colors+';">'+@colors+'</span>', "id", "name")