options_for_select(container, selected = nil)
容器是显示\值组合,所以你需要[[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
选项的对象的集合(作为值),其中选择已:phase_names = :value
f.collection_select :phase_names, [:name => "rStage", :value => "Pre Contract"] , :value, :name, {:include_blank => 'Select one'}
刚刚工作方式的ActiveRecord类
感谢,这是我的尝试: <%= f.select:PH ase_names,options_for_select([[@ phase_names =:name],“RFP Stage”,“Pre Contract”,“Awarded”,“Unsuccessful”,“Completed”]),:class =>'inputboxes'%> 但是,显示的是“名”,现在在选择 – Clay
phase_names是从第二个数据库表遗憾忘了前面提到,所以每个项目都像“标记”编辑问题的阶段 纠正 – Clay
此选项没有在数据库中存储 <%= collection_select(:phase_names,:phase_id,Phase.all,:id,:name,{:include_blank => true})%> – Clay