options_for_select(container, selected = nil)
容器是 display\value 组合,所以你需要[[value,name],[value,name]]
或者如果它相同[name]
喜欢["RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"]
现在您有了容器,您需要一些东西来匹配所选值
@current_value = MyModel.find(1).vari # Assume MyModel table with id has col vari=Completed
然后,你可以做
select_tag "select_name", options_for_select(["RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"].insert(0, "Select One"), @current_value)
另一种方法是拥有一个对象集合,其中包含选项,让我们说:name
(显示)和:value
(用作值)其中 selected 有:phase_names = :value
f.collection_select :phase_names, [:name => "rStage", :value => "Pre Contract"] , :value, :name, {:include_blank => 'Select one'}
就像 activerecord 类一样工作