2012-04-26 24 views
2

我在軌初學者,我有這個陣列的時候:如何將一個字符串數組轉換爲下拉列表框?

[ 「08:30」, 「09:00」, 「09:30」, 「10:00」,「10: 30「,」11:00「,」11:30「, 」12:00「,」12:30「,」13:00「,」13:30「,」14:00「,」14:30 「,」15:00「, 」15:30「,」16:00「,」16:30「,」17:00「,」17:30「,」18:00「,」18:30「 , 「19:00」, 「19:30」, 「20:00」, 「20:30」, 「21:00」, 「21:30」]

我怎麼能轉換成使用options_from_collection_for_select下拉菜單。我試着用

<%= select_tag "start_time", options_from_collection_for_select(@start_time,:id, :name) %>

但它返回"undefined method 'name' for "08:30":String"

回答

5

假設

@options = ["08:30", "09:00", "09:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30"] 

代碼將是

<%= select_tag "start_time", options_for_select(@options) %> 

參見options_for_select文檔。

+0

感謝。有效! – 2012-04-26 10:21:51

0

您可以使用像下面

<%= f.select :time, options_for_select(MONTHS, @job.time) %> 

個月=>您的陣列

相關問題