验证:什么东西,:确认=>真放; attr_accessor混乱 [英] validates :something, :confirmation => true & attr_accessor confusion
问题描述
我挣扎着红宝石验证:确认=>在我的Rails应用程序如此。请看下面的code:
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 }
结束
由于code是上面的,我的验证工作正常,从Rails的控制台:
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">
但都通过了RSpec和手动滑轨服务器测试会导致验证失败,说他们不匹配时,他们织补好做。如果我取消了attr_accessor为:针,将验证会通过,但:引脚当然不会被写入到数据库中。
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.
我完全相信我失去了一些东西明显,vital-只是运行到砖墙。
I'm completely sure I'm missing something obvious and vital- just running into a brick wall.
推荐答案
就像弗雷德里克上面说的,这个问题是比较String的实例与整数的一个实例。
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:
Thing.new(params[:thing]) # note all these params come in as a string
发生了什么事是,由于#pin是一个整数列,您将获得以下行为:
What's happening is that since #pin is an integer column, you will get the following behaviour:
my_thing = Thing.new
my_thing.pin = "123456"
my_thing.pin # Will be the integer 123456, the attribute has been auto-cast for you
但由于#pin_confirmed只是一个普通的属性,而不是一个整数列,这里的古怪,你会看到:
But since #pin_confirmed is just a regular attribute, not an integer column, here's the weirdness you will see:
my_thing = Thing.new
my_thing.pin_confirmation = "123456"
my_thing.pin_confirmation # Will be the *string* "123456", the attribute has been set as is
所以很自然,在这种情况下,无论什么样的价值观,你有,因为他们在通过PARAMS散列(这始终是一组字符串),你最终会指定的字符串值,这两个属性,但他们将被转换为不同的类型。
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.
一,您可以创建#pin_confirmation在数据库中一个整数列。
One, you could create #pin_confirmation as an integer column in the database.
另一种是,你可以为以下形式的#pin_confirmation添加属性设置器:
The other is you could add an attribute setter for #pin_confirmation of the following form:
def pin_confirmation=(val)
@pin_confirmation = val.to_i
end
这篇关于验证:什么东西,:确认=&GT;真放; attr_accessor混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!