validates_inclusion_of no longer working the same in Rails 4.1?

前端 未结 3 1328
花落未央
花落未央 2020-12-31 20:29

The following code made sure that a time_zone chose is within the time zones in ActiveSupport::TimeZone.us_zones:

validates_inclusi         


        
相关标签:
3条回答
  • 2020-12-31 21:18

    try adding .keys ?

    validates :time_zone, 
      inclusion: { 
        in: ActiveSupport::TimeZone.zones_map.keys 
      } 
    
    0 讨论(0)
  • 2020-12-31 21:20

    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"
    
    0 讨论(0)
  • 2020-12-31 21:33

    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) }
    
    0 讨论(0)
提交回复
热议问题