How can I blacklist usernames with Devise?

2019-05-10 06:36发布

问题:

I have Devise setup to allow login with email or username. With your username you can have a vanity URL like so: vanity.com/username. My User model thus has attr_accessible :username as well as attr_accessor :login.

To prevent usernames from colliding with future features, I want to implement a blacklist on certain usernames. You can see a nice example list in use by GitHub here.

I'm new to Devise and have searched the how-to's in their wiki to see if this use case or anything like it is covered there. It doesn't seem to be.

How can I blacklist certain usernames for registration in Devise?

回答1:

There was an easier way to do this than having to modify Devise's controllers.

In the User model all I had to do was:

validates :username, :exclusion => %w(about blog ...)

Way too simple.