Reputation: 8730
Drop down for timezones in form
<%= time_zone_select :time_zone, ActiveSupport::TimeZone.all, nil,
{:include_blank => false,:prompt=>"Select Time Zone"} %>
After selecting some timezone and submitting form, when I do params["time_zone"] I get
"#<ActiveSupport::TimeZone:0x00000001ff5450 @name=%22American Samoa%22, @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Pacific/Pago_Pago>,
@current_period=#<TZInfo::TimezonePeriod: #<TZInfo::TimezoneTransitionDefinition: #<TZInfo::TimeOrDateTime: 439038000>,#<TZInfo::TimezoneOffset: -39600,0,SST>>,nil>>,
#<ActiveSupport::TimeZone:0x00000002024bb0 @name=%22International Date Line West%22, @utc_offset=nil, @tzinfo=#<TZInfo::TimezoneProxy: Pacific/Midway>,..............
How I get selected timezone? Note: I save timezone in string
Upvotes: 2
Views: 1951
Reputation: 102036
Simply call .name
on the ActiveSupport::TimeZone
object:
irb(main):055:0> ActiveSupport::TimeZone.new("American Samoa").name
=> "American Samoa"
You can can do this with a custom setter. Example:
class City < ActiveRecord::Base
# automatically convert ActiveSupport::TimeZone
# objects into a serializable string.
def time_zone=(tz)
super(tz.try(:name) || tz)
end
end
class CitiesController
def create
@city = City.create(city_params)
respond_with(@city)
end
def city_params
params.require(:city).permit(:time_zone)
end
end
Upvotes: 2
Reputation: 53
It looks like your time_zone_select
is actually called time_zone
rather than timezone
so try doing params['time_zone']
.
Upvotes: 0