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
精彩评论