Rails 教程区分大小写不起作用
我正在遵循 Rails 教程,在注册我的网站后,我尝试登录。我最近在我的应用程序中注意到,如果我大写我的电子邮件地址,我会收到无效的用户名/密码消息。我在 Rubular 上测试了正则表达式,它适用于大写,所以不可能是这样。
也许这与会话有关?
email_regex = /\A[\w+\-.]+@[csupomona\d\-.]+[edu]+\z/i
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
这是会话/创建的代码
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in user
redirect_to root_path
end
end
I am following Rails tutorial and after signing up for my site, I am trying to sign in. I recently noticed in my app that if I capitalize my email address, I get the invalid username/password message. I tested the regex on Rubular and it works with capitalization so that can't be it.
Maybe this deals with sessions?
email_regex = /\A[\w+\-.]+@[csupomona\d\-.]+[edu]+\z/i
validates :email, :presence => true,
:format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
Here's the code for sessions/create
def create
user = User.authenticate(params[:session][:email],
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Sign in"
render 'new'
else
sign_in user
redirect_to root_path
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与验证无关。
它与
User.authenticate
如何工作(我假设它执行 User.find_by_email)查看问题,我从来没有真正遇到过这个问题。
但要解决这个问题,有几种方法可以解决。
User.authenticate
执行相同操作User.authenticate
不区分大小写。但我有一种感觉
User.authenticate
来自Devise
或类似的东西。所以1.可能是最好的解决方案This is nothing to do with validation.
It is to do with
User.authenticate
works (I assume it does a User.find_by_email)Looking at the question, I've never actually had this be a problem.
But to solve it there are a couple of ways to go about this.
User.authenticate
do the sameUser.authenticate
is case insensitive.But I've got a feeling that
User.authenticate
is coming fromDevise
or something similar. So 1. may be the best solution您对正则表达式的用途有点困惑。让我们看一下:
正则表达式用在哪里?它用于
:format
,因此正则表达式仅用于查看传入电子邮件地址是否看起来像有效的电子邮件地址,而与唯一性无关。validates
的:uniqueness
参数指定在确保电子邮件地址唯一时不应考虑大小写。因此,您不能拥有两个电子邮件地址仅大小写不同的用户。您所显示的代码中没有任何内容涉及当您尝试让某人登录时如何比较电子邮件地址,这就是您遇到麻烦的地方。您必须更新您的
User.authenticate
类方法才能对电子邮件地址进行不区分大小写的搜索。You're a little confused about what that regex is for. Let us have a look at this:
Where is the regex being used? It is being used for the
:format
so the regex is only used to see if an incoming email address looks like a valid email address and that has nothing to do with uniqueness. The:uniqueness
parameter tovalidates
specifies that case shouldn't be considered when ensuring that email addresses are unique. So you can't have two users whose email addresses differ only by case.Nothing in the code you've shown talks about how email addresses are compared when you're trying to sign someone in and that's where you're have trouble. You'll have to update your
User.authenticate
class method to do a case insensitive search for the email address.