I am trying to prevent usernames from having specific characters so they can be used in pathways. Basically I want to use pathways such as www.mysite.com/profiles/username and allowing stuff like user.name or user/name will cause errors. I was originally think of using validates_exclusion_开发者_如何学运维of
, but am not sure if this allows to exclude characters rather than whole words. I am also wondering if it would be a better practice to whitelist rather than blacklist. For example, I could whitelist 0..9 and A..Z.
Does anyone know of any "standard" or best practice approaches to this?
Standard and best approach for this is to use friendly_id. But if you don't want to increase dependencies of your project, and want to only allow alphanumeric:
validates :user_name,
:format => {:with => /^\w+$},
:presence => true,
:uniqueness => true
精彩评论