3
votes

In SimpleForm you can create a timezone input:

= simple_form_for @user do |f|
  = f.input :time_zone, label: 'My time zone'

However the values which simple form puts into the form are not valid for Postgres:

<option value="American Samoa">(GMT-11:00) American Samoa</option>
<option value="International Date Line West">(GMT-11:00) International Date Line West</option>
<option value="Midway Island">(GMT-11:00) Midway Island</option>
<option value="Hawaii">(GMT-10:00) Hawaii</option>
<option value="Alaska">(GMT-09:00) Alaska</option>
<option value="Pacific Time (US &amp; Canada)">(GMT-08:00) Pacific Time (US &amp; Canada)</option>
<option value="Tijuana">(GMT-08:00) Tijuana</option>
...

The actual values need to correspond with the hash:

{"International Date Line West" => "Pacific/Midway", 
"Midway Island" => "Pacific/Midway", 
"American Samoa" => "Pacific/Pago_Pago", 
"Hawaii" => "Pacific/Honolulu", 
"Alaska" => "America/Juneau", 
"Pacific Time (US & Canada)" => "America/Los_Angeles", 
"Tijuana" => "America/Tijuana",
...}

How can you get the simple_form time_zone field to have the values in the correction "region/locality" format?

2

2 Answers

4
votes

Rails time zones are an oversimplification of the standard TZDB. If you can avoid using them, I would use the full IANA TZDB zones instead. In Ruby, this is easiest with the TZInfo Gem.

If you are stuck with them, then you can use the MAPPING constant from ActiveSupport::TimeZone, as shown in these docs. That will let you correlate the Rails identifier back to the normal region/locality format.

See also the section at the bottom of the timezone tag wiki on Rails time zones.

1
votes

You can try this trick:

f.input :time_zone, model: TZInfo::Timezone

but this will not render option names as you want, only values.