验证:某事,:确认=>真实& attr_accessor 混淆
我正在与 Ruby 验证作斗争:confirmation =>在我的 Rails 应用程序中是这样。考虑以下代码:
# == Schema Information
#
# Table name: things
#
# id :integer not null, primary key
# pin :integer(8)
# created_at :datetime
# updated_at :datetime
#
class Things < ActiveRecord::Base
#attr_accessor :pin
attr_accessible :pin, :pin_confirmation
validates :pin,
:confirmation => true,
:length => { :within => 7..15 },
:numericality => { :only_integer => true }
正如
上面的代码,我的验证在 Rails 控制台中工作正常:
1.9.3-p0 :002 > l2 = Thing.create! :pin => 1234567, :pin_confirmation => 1111111
ActiveRecord::RecordInvalid: Validation failed: Pin doesn't match confirmation
....
1.9.3-p0 :003 > l2 = Thing.create! :pin => 1234567, :pin_confirmation => 1234567
=> #<Thing id: 2, pin: 1234567, created_at: "2012-01-30 22:03:29", updated_at: "2012-01-30 22:03:29">
但是通过 rspec 和手动从 Rails 服务器进行测试会导致验证失败,说它们在表现得很好时不匹配。如果我取消注释 :pin 的 attr_accessor,验证将通过,但 :pin 当然不会写入数据库。
我完全确定我错过了一些明显且重要的东西——只是碰上了砖墙。
am struggling with Ruby validates :confirmation => true in my Rails app. Consider the following code:
# == Schema Information
#
# Table name: things
#
# id :integer not null, primary key
# pin :integer(8)
# created_at :datetime
# updated_at :datetime
#
class Things < ActiveRecord::Base
#attr_accessor :pin
attr_accessible :pin, :pin_confirmation
validates :pin,
:confirmation => true,
:length => { :within => 7..15 },
:numericality => { :only_integer => true }
end
As the code is above, my validation works fine from the Rails console:
1.9.3-p0 :002 > l2 = Thing.create! :pin => 1234567, :pin_confirmation => 1111111
ActiveRecord::RecordInvalid: Validation failed: Pin doesn't match confirmation
....
1.9.3-p0 :003 > l2 = Thing.create! :pin => 1234567, :pin_confirmation => 1234567
=> #<Thing id: 2, pin: 1234567, created_at: "2012-01-30 22:03:29", updated_at: "2012-01-30 22:03:29">
but testing both through rspec and manually from rails server causes the validation to fail, saying they don't match when they darn well do. If I uncomment the attr_accessor for :pin, the validations will pass but the :pin of course will not be written to the database.
I'm completely sure I'm missing something obvious and vital- just running into a brick wall.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Frederick 上面所说,问题是将 String 实例与 Integer 实例进行比较。
很可能,这就是您控制器中的内容:
发生的情况是,由于 #pin 是整数列,因此您将得到以下行为:
但由于 #pin_confirmed 只是一个常规属性,而不是整数列,因此您会感到奇怪会看到:
所以自然地,在这种情况下,无论您有什么值,因为它们是通过“params”散列(始终是一组字符串)传入的,您最终都会为这两个属性分配字符串值,但它们将被投射到不同类型。
有几种方法可以解决这个问题。
一,您可以在数据库中创建 #pin_confirmation 作为整数列。
另一种是您可以为 #pin_confirmation 添加以下形式的属性设置器:
Like Frederick said above, the issue is comparing an instance of String with an instance of Integer.
More than likely, here's what you have in your controller:
What's happening is that since #pin is an integer column, you will get the following behaviour:
But since #pin_confirmed is just a regular attribute, not an integer column, here's the weirdness you will see:
So naturally, in that case, no matter what values you have, since they come in through the "params" hash (which is always a set of strings), you will end up assigning string values to both attributes, but they will be cast to different types.
There are several ways to fix this.
One, you could create #pin_confirmation as an integer column in the database.
The other is you could add an attribute setter for #pin_confirmation of the following form: