选择显示当前选定的选项

时间:2012-06-21 21:19:05

标签: ruby-on-rails select highlight

我正在使用rails并拥有此代码:

<%= select_tag :tag, options_for_select(["Politics", "Technology", "Entertainment", 
   "Sports", "Science", "Crime", "Business", "Social", "Nature", "Other"]), :selected => 
    "Politics" %>

:selected部分显然不是一个有效的选项,但它会传达我想做的事情。代替"Politics"应该是当前选择的选项(我知道如何做这部分)。但更重要的是,默认情况下突出显示选项的选项是什么?

1 个答案:

答案 0 :(得分:3)

默认选择的选项在option_for_select方法

中说明

方法接受的第二个属性是应该选择哪个元素。

options_for_select(container, selected = nil)

所以在你的情况下,代码将是:

<%= select_tag :tag, options_for_select(["Politics", "Technology", "Entertainment", 
   "Sports", "Science", "Crime", "Business", "Social", "Nature", "Other"], "Politics") %>