Ruby on rails ActiveRecord validtaes_格式';你不会被处决吗?

Ruby on rails ActiveRecord validtaes_格式';你不会被处决吗?,ruby-on-rails,ruby,validation,activerecord,Ruby On Rails,Ruby,Validation,Activerecord,AR中有Person类和PersonName类,它们之间有很多关系。现在我想验证PersonName模型的格式是字母还是空格。代码如下: class Person < ActiveRecord::Base has_many :names, :class_name => "PersonName", :dependent => :destroy accepts_nested_attributes_for :names end class PersonName < A

AR中有Person类和PersonName类,它们之间有很多关系。现在我想验证PersonName模型的格式是字母还是空格。代码如下:

class Person < ActiveRecord::Base
  has_many :names, :class_name => "PersonName", :dependent => :destroy
  accepts_nested_attributes_for :names
end

class PersonName < ActiveRecord::Base
  belongs_to :person
  attr_accessible :full, :first, :middle, :last, :maiden, :title, :suffix, :nick
  validates_format_of :full, :first, :middle, :last, :maiden, :title, :suffix, :nick, :with => /^[a-zA-Z\s]*$/, :on => :save
end

在控制台中,它返回true。我试图将其更改为:on=>:create,但仍然返回true。有什么问题吗?我需要在Person类中指定什么吗?

来自rails文档

Note: use \A and \Z to match the start and end of the string, ^ and $ match the start/end of a line

我想那是你的问题

我找到了原因,应该用create!而不是创建

请尝试
PersonName.new(:full=>'@#$#')。有效吗?
。它是否仍然返回
true
?是的,它仍然返回true。它是
\z
(带小写字母“z”)。感谢您的提示,我认为您是对的(/a和/z表示字符串),但是/\a[a-zA-z\s\.]*\z/并不能解决问题。
Note: use \A and \Z to match the start and end of the string, ^ and $ match the start/end of a line