与大多数网站的工作方式相同,我将“UsErNaMe”存储在数据库中,但让用户使用“用户名”登录。
这是一个相当明显且必要的功能,很多人似乎都问过它,但我不断遇到的解决方案似乎与 Devise 自己的文档脱节。
例如,考虑这篇博文:http://anti-pattern.com/2011/5/16/case-insensitive-keys-with-devise http://anti-pattern.com/2011/5/16/case-insensitive-keys-with-devise
[...]您可能遇到过一些用户喜欢输入的问题
登录信息(电子邮件和/或用户名)中的某些字母为大写,
但希望他们尝试登录时不区分大小写。
无理要求[...]
凉爽的!这就是我想要的。
他的解决方案:
# config/initializers/devise.rb
Devise.setup do |config|
config.case_insensitive_keys = [:email, :username]
end
这就是我不断寻找的解决方案。但这里是该配置选项的文档:
# Configure which authentication keys should be case-insensitive.
# These keys will be downcased upon creating or modifying a user and when used
# to authenticate or find a user. Default is :email.
config.case_insensitive_keys = [ :username, :email ]
特别是:“这些键将在创建/修改用户时小写。”换句话说,用户名在数据库中被小写。
核实:
User.create username: "UsErNaMe", password: "secret", email: "[email protected] /cdn-cgi/l/email-protection"
#=> <User username="username"...>
我是否遗漏了一些非常明显的事情?