下拉表格中的时区
<%= time_zone_select :time_zone, ActiveSupport::TimeZone.all, nil,
{:include_blank => false,:prompt=>"Select Time Zone"} %>
选择一些时区并提交表格后,当我做params [“time_zone”]时,我得到了
"#<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>,..............
我如何获得选定的时区? 注意:我在字符串中保存时区
答案 0 :(得分:2)
只需在.name
对象上调用ActiveSupport::TimeZone
:
irb(main):055:0> ActiveSupport::TimeZone.new("American Samoa").name
=> "American Samoa"
您可以使用自定义设置器执行此操作。例如:
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
答案 1 :(得分:0)
看起来您的time_zone_select
实际上是time_zone
而不是timezone
,所以请尝试params['time_zone']
。