Unexpected timezone format with options_for_select and simple_form_for in Rails. #48

Closed
adrianmann opened this Issue Feb 16, 2016 · 2 comments

Comments

Projects
None yet
2 participants
@adrianmann

I am using an f.select tag to display a drop down of timezones. However, the format displayed is not what is expected. For example, instead of "Europe/Dublin", I am getting "Europe - Dublin"?

The following placed inside a simple_form_for block:

<%= f.label :timezone %>
<%= f.select :timezone, options_for_select(TZInfo::Timezone.all.each) %>

returns timezones with the unexpected format, whereas a <select> tag displays them as expected.

How can I correct this issue so the format is returned in the expected way?

@adrianmann adrianmann changed the title from Incorrect/unexpected timezone format. to Unexpected timezone format with options_for_select and simple_form_for in Rails. Feb 16, 2016

@philr

This comment has been minimized.

Show comment
Hide comment
@philr

philr Feb 16, 2016

Member

You are seeing the result of options_for_select calling TZInfo::Timezone#to_s on each Timezone.

TZInfo::Timezone.all_identifiers returns an array of identifiers and could be used instead of all:

<%= f.select :timezone, options_for_select(TZInfo::Timezone.all_identifiers) %>

You could also call TZInfo::Timezone#identifier on each Timezone:

<%= f.select :timezone, 
      options_from_collection_for_select(TZInfo::Timezone.all, :identifier, :identifier) %>
Member

philr commented Feb 16, 2016

You are seeing the result of options_for_select calling TZInfo::Timezone#to_s on each Timezone.

TZInfo::Timezone.all_identifiers returns an array of identifiers and could be used instead of all:

<%= f.select :timezone, options_for_select(TZInfo::Timezone.all_identifiers) %>

You could also call TZInfo::Timezone#identifier on each Timezone:

<%= f.select :timezone, 
      options_from_collection_for_select(TZInfo::Timezone.all, :identifier, :identifier) %>

@philr philr added the question label Feb 16, 2016

@philr philr closed this Feb 16, 2016

@adrianmann

This comment has been minimized.

Show comment
Hide comment

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment