表单中的时区下拉列表
<%= time_zone_select :time_zone, ActiveSupport::TimeZone.all, nil,
{:include_blank => false,:prompt=>"Select Time Zone"} %>
选择一些时区并提交表单后,当我做参数["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>,..............
我如何获得选定的时区?注意:我将时区保存在字符串中
只需在ActiveSupport::TimeZone
对象上调用.name
:
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
看起来你的time_zone_select
实际上被称为time_zone
而不是timezone
所以试着做params['time_zone']
。