How do you configure devise to accept either an email OR a username to login?
Out of the box, the devise gem asks for a开发者_如何学Go user's email address and uses that to sign in (authenticate).
This railscast, "Customizing Devise", describes clearly how one can authenticate with a username in lieu of an email. How would one configure devise to try to authenticate against two different columns, either username OR email?
You just need define the method find_for_database_authentication(conditions). By example, with monogid I do that :
def self.find_for_database_authentication(conditions)
self.where({ :login => conditions[:login] }).first || self.where({ :email => conditions[:login] }).first
end
Ah, sorry, I didn't google sufficiently. There's an answer on the devise wiki: "Log in using login or mail"
Also, this question was already asked and answered on stackoverflow here: "Sign in with username OR email"
There's a further blog discussioon that uses the self.find_for_authentication method which is Rails 2.x compatible.
精彩评论