ruby-on-railsrubydatetimetzinfo

Set a default timezone in a Rails form


I am trying to set a default, or ideally first show a predefined list of options, timezone in a form when creating a company.

Currently, the code below renders an entire list of available timezones, and finding a specific timezone is rather tedious. All of the current companies are either in one of two timezones, and it would be a better experience to default to a specific timezone, or even better, append two timezones values to the top of the list. Here is the form field:

= form.select :time_zone, (TZInfo::Timezone.all.map { |tz| ["(UTC #{tz.current_period.offset.utc_offset/3600}) #{tz.to_s}", tz.name] })

I've tried to set a few different options but there doesn't seem to be a conventional way to do so.


Solution

  • Maybe this API document is what you need.

    http://apidock.com/rails/v4.2.7/ActionView/Helpers/FormOptionsHelper/options_for_select

    form.select :time_zone, options_for_select(TZInfo::Timezone.all.map { |tz| ["(UTC #{tz.current_period.offset.utc_offset/3600}) #{tz.to_s}", tz.name] }, selected: "your default option")