validates_inclusion_of 在 Rails 4.1 中不再起作用? [英] validates_inclusion_of no longer working the same in Rails 4.1?

查看:32
本文介绍了validates_inclusion_of 在 Rails 4.1 中不再起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码确保选择的 time_zoneActiveSupport::TimeZone.us_zones 中的时区内:

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

validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.zones_map(&:name)

在 Rails 4.0 中运行良好.刚刚升级到 Rails 4.1,我的索引页面上出现了这个错误(所以只需查看模型):

Worked great in Rails 4.0. Just upgraded to Rails 4.1 and I'm getting this error on my index page (so just simply viewing the models):

具有#include 方法的对象?或 proc、lambda 或符号是必需,并且必须作为 :in (或 :within)选项提供配置哈希

An object with the method #include? or a proc, lambda or symbol is required, and must be supplied as the :in (or :within) option of the configuration hash

我猜测,ActiveSupport::TimeZone.zones_map(&:name) 不再是 in 属性的有效值?

I'm guessing from that, ActiveSupport::TimeZone.zones_map(&:name) is no longer a valid value for the in property?

推荐答案

尝试添加 .keys 吗?

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

这篇关于validates_inclusion_of 在 Rails 4.1 中不再起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆