【问题标题】:Time zone dropdown where select value is index选择值为索引的时区下拉菜单
【发布时间】:2016-01-04 03:24:11
【问题描述】:

我正在尝试在我的用户配置文件中为 time_zones 创建一个选择下拉列表,但我需要匹配列表和索引,因为我没有使用文本,我使用整数来将区域存储在我的数据库中.我这样做是为了在 rails admin 中将其显示为下拉菜单,而不是文本输入。

用户:

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

    尝试这里描述的 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 应该会为您提供第二个参数,该参数将包含您的哈希索引。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      相关资源
      最近更新 更多