如何将字符串数组转换为rails中的下拉列表?

时间:2012-04-26 10:13:37

标签: ruby-on-rails

我是rails的初学者,我有这么多次:

  

[“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"

2 个答案:

答案 0 :(得分:6)

假设

@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文档。

答案 1 :(得分:0)

您可以使用以下

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

MONTHS =&gt;你的阵列