instance method time_zone_options_for_select

Ruby on Rails 5.2.8.1

Since v2.2.3

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12 v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone)

Returns a string of option tags for pretty much any time zone in the world. Supply an ActiveSupport::TimeZone name as selected to have it marked as the selected option tag. You can also supply an array of ActiveSupport::TimeZone objects as priority_zones, so that they will be listed above the rest of the (long) list. (You can use ActiveSupport::TimeZone.us_zones as a convenience for obtaining a list of the US time zones, or a Regexp to select the zones of your choice)

The selected parameter must be either nil, or a string that names an ActiveSupport::TimeZone.

By default, model is the ActiveSupport::TimeZone constant (which can be obtained in Active Record as a value object). The only requirement is that the model parameter be an object that responds to all, and returns an array of objects that represent time zones.

NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.

Parameters

selected opt = nil
priority_zones opt = nil
model opt = ::ActiveSupport::TimeZone
Source
# File actionview/lib/action_view/helpers/form_options_helper.rb, line 575
      def time_zone_options_for_select(selected = nil, priority_zones = nil, model = ::ActiveSupport::TimeZone)
        zone_options = "".html_safe

        zones = model.all
        convert_zones = lambda { |list| list.map { |z| [ z.to_s, z.name ] } }

        if priority_zones
          if priority_zones.is_a?(Regexp)
            priority_zones = zones.select { |z| z =~ priority_zones }
          end

          zone_options.safe_concat options_for_select(convert_zones[priority_zones], selected)
          zone_options.safe_concat content_tag("option".freeze, "-------------", value: "", disabled: true)
          zone_options.safe_concat "\n"

          zones = zones - priority_zones
        end

        zone_options.safe_concat options_for_select(convert_zones[zones], selected)
      end

Defined in actionview/lib/action_view/helpers/form_options_helper.rb line 575 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::FormOptionsHelper

Type at least 2 characters to search.

↑↓ navigate · open · esc close