2012-07-03 29 views
0

我有以下爲我的f選擇F選擇顯示數據庫中的當前值

如何修改以顯示數據庫中的內容?

<%= f.select :phase_names, options_for_select([["Select One", "", @phase_names_string_value], "RFP Stage", "Pre Contract", "Awarded", "Unsuccessful", "Completed"]), :class => 'inputboxes' %> 

相名稱是從所述數據庫中的第二個表。然而,每個項目一次只能坐在一個階段。

在此先感謝

回答

0
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類

+0

感謝,這是我的嘗試: <%= f.select:PH ase_names,options_for_select([[@ phase_names =:name],「RFP Stage」,「Pre Contract」,「Awarded」,「Unsuccessful」,「Completed」]),:class =>'inputboxes'%> 但是,顯示的是「名」,現在在選擇 – Clay

+0

phase_names是從第二個數據庫表遺憾忘了前面提到,所以每個項目都像「標記」編輯問題的階段 糾正 – Clay

+0

此選項沒有在數據庫中存儲 <%= collection_select(:phase_names,:phase_id,Phase.all,:id,:name,{:include_blank => true})%> – Clay

相關問題