0
我正在编写一个应用程序,用户必须至少13岁才能加入。我的设置是Devise和Unicorn server,这是我User
型号:我如何只允许某一年龄的用户加入?
User.rb
attr_accessible :birthday
validates_presence_of :birthday
validate :is_certain_age
private
def is_certain_age
if self.birthday
errors.add(:base, 'You need to be at least 13.') if self.birthday > Date.today.year - 13
end
end
我试图测试这个浏览器,但是当我提交了报名表,它POST并保持不创建用户或不创建它。我不知道发生了什么,因为独角兽服务器不会告诉我像WEBrick那样的参数。这是我得到的:
127.0.0.1 - - [22/Jun/2013 11:21:12] "POST /users HTTP/1.1" 200 - 0.3460
我Rspec的测试,为用户显示这些对故障:
User
Failure/Error: it { should be_valid }
NoMethodError:
undefined method `to_datetime' for 2000:Fixnum
注:使用Shoulda Matchers
User valid user age
Failure/Error: it { should ensure_inclusion_of(:birthday).in_range(13..150) }
Did not expect errors to include "is not included in the list".....
...when birthday is set to 12, got error:
我做了什么错?
谢谢,我最终改变了我的验证,而不是:'validates_inclusion_of:birthday,:in => Date.new(1900).. Time.now.years_ago(13).to_date,:message =>'对不起,你必须年满13岁才能加入。感谢您的澄清。 – LearningRoR