When I try:
validates_inclusion_of :time_zone, :in => TimeZone
validates_inclusion_of :time_zone, :in => Time.zone
This error appears:
"<class:User>": uninitialized constant User::TimeZone (NameError)
I’m trying to let users select any time zone of the world but since I’m based in the U.S.A. my select menu is this:
<%= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones, {:prompt => "Select Your Time Zone *"}, {:id => "timezone"} %>
What’s the correct way to do this?
Thank you.
If you select the option with value
"(GMT-05:00) Eastern Time (US & Canada)"this string is going to be passed to the model for validation. Yourvalidates_inclusion_ofis going to runEnum‘s.include?method on the collection you pass with:in.Neither
TimezoneandTime.zoneextendEnumto my knowledge, so they will not return anEnuminstance that.include?will return true/false for.If your select consists of
ActiveSupport::TimeZone.us_zones, this is what you should be checking the inclusion validator againstBut as
ActiveSupport::TimeZone.us_zonesdoesn’t return strings, one way you could get a common type for comparison is casting the aboveEnum‘s contents to strings.With this, a selected value like
"(GMT-05:00) Eastern Time (US & Canada)"should evaluatetrue, as the following does in console without trouble.