validates_inclusion_of no longer working the same in Rails 4.1?

梦想与她 提交于 2019-11-30 09:30:19
house9

try adding .keys ?

validates :time_zone, 
  inclusion: { 
    in: ActiveSupport::TimeZone.zones_map.keys 
  } 

In Rails 5, ActiveSupport::TimeZone.zones_map is a private method. Therefore, if you want your validation to work, I suggest the following syntax:

validates :time_zone, inclusion: { in: ActiveSupport::TimeZone.all.map(&:name) }

If you want to keep using validates_inclusion_of this works as well:

validates_inclusion_of :time_zone, 
   :in => ActiveSupport::TimeZone.zones_map(&:name).keys, 
   :message => "is not a valid time zone"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!