2015-10-07 74 views
0

我試圖爲我的用戶配置文件中的time_zones創建選擇下拉列表,但我需要列表和索引進行匹配,因爲我沒有使用文本,我正在使用整數用於將區域存儲在我的數據庫中。我這樣做,所以我可以顯示它作爲下拉欄管理員而不是文本輸入。時區下拉列表中選擇值是索引

用戶:

validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.us_zones.map { |z| z.name }, 
message: "is not a valid time zone", 
allow_blank: true 

# This allows me to display the time_zone as a dropdown in rails admin instead of a text box 
TIMEZONES = ActiveSupport::TimeZone.us_zones.map { |z| z.name } 
def time_zone_enum 
    TIMEZONES.each_with_index.to_a 
end 

查看:這裏所描述

<%= f.collection_select :time_zone, ActiveSupport::TimeZone.us_zones.map { |z| z.name }, value, name %> 

# I don't know how to get the value to be an index of the map operation and to display the name 

回答

0

嘗試map.each_with_index:How to map with index in Ruby?

<%= f.collection_select :time_zone, ActiveSupport::TimeZone.us_zones.map.each_with_index { |z, i| i.to_s.concat(" #{z.name}") }, name %> 

您可能需要修改該代碼以在您的Rails應用程序中工作,但在您的映射上調用each_with_index應該會給您第二個參數,該參數將包含您的哈希索引。