开发者

Rails validations format and changing any other characters into hyphens?

I have this model:

class Tag < ActiveRecord::Base

  # Validations
  validates :name, :presence => true, :uniqueness => true, :format => /[a-z0-9\+\-]+/

end

开发者_Python百科What I want my app to do is to change every character that is not [a-z0-9\+\-] to be turned into a hyphen. Is this possible, and how? Thanks.


You could use a before filter to convert it pre-validation...

before_validation :convert_name

def convert_name
  self.name.gsub! /[^a-z0-9\+\-]/, '-'
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜