Rails 教程区分大小写不起作用

发布于 2024-12-11 10:42:29 字数 778 浏览 0 评论 0原文

我正在遵循 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你到最终 2024-12-18 10:42:29

这与验证无关。
它与

  1. 电子邮件地址如何存储在数据库中(假设一些大写,一些小写)有关
  2. User.authenticate 如何工作(我假设它执行 User.find_by_email)

查看问题,我从来没有真正遇到过这个问题。

但要解决这个问题,有几种方法可以解决。

  1. 将所有电子邮件存储为小写,并让 User.authenticate 执行相同操作
  2. 确保 User.authenticate 不区分大小写。

但我有一种感觉 User.authenticate 来自 Devise 或类似的东西。所以1.可能是最好的解决方案

class User

  # all email writes get lowercased
  def email=(value)
    self[:email] = value && value.downcase
  end

end

user = User.authenticate(params[:session][:email].try(:downcase), ...)

This is nothing to do with validation.
It is to do with

  1. How email addresses are stored in the database (assumedly some upper case, some lower case)
  2. How the 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.

  1. Store all emails lowercase, and make User.authenticate do the same
  2. Ensure that User.authenticate is case insensitive.

But I've got a feeling that User.authenticate is coming from Devise or something similar. So 1. may be the best solution

class User

  # all email writes get lowercased
  def email=(value)
    self[:email] = value && value.downcase
  end

end

user = User.authenticate(params[:session][:email].try(:downcase), ...)
北恋 2024-12-18 10:42:29

您对正则表达式的用途有点困惑。让我们看一下:

validates :email, :presence   => true,
                  :format     => { :with => email_regex },
                  :uniqueness => { :case_sensitive => false }

正则表达式用在哪里?它用于 :format,因此正则表达式仅用于查看传入电子邮件地址是否看起来像有效的电子邮件地址,而与唯一性无关。 validates:uniqueness 参数指定在确保电子邮件地址唯一时不应考虑大小写。因此,您不能拥有两个电子邮件地址仅大小写不同的用户。

您所显示的代码中没有任何内容涉及当您尝试让某人登录时如何比较电子邮件地址,这就是您遇到麻烦的地方。您必须更新您的 User.authenticate 类方法才能对电子邮件地址进行不区分大小写的搜索。

You're a little confused about what that regex is for. Let us have a look at this:

validates :email, :presence   => true,
                  :format     => { :with => email_regex },
                  :uniqueness => { :case_sensitive => false }

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 to validates 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文