Rails:使用用户注册/登录验证

发布于 2024-09-29 03:34:47 字数 651 浏览 0 评论 0原文

我有一个简单的 User 类,具有以下名称唯一性验证:

class User < ActiveRecord::Base

  validates :name, :uniqueness => true,

创建新用户时它效果很好。然而,当我检查登录表单时,用户输入了他的名字,系统说它已经被占用,这没有任何意义。

所以我实现了一个单独的 valid_login? 方法,但是我无法在那里进行唯一性检查:

def valid_login?
  validates :name, :uniqueness => false # doesn't work
end

这是我的控制器的代码:

def login
    return unless request.post?

    @user = User.new(params[:user])
    if @user.valid_login?
      # Redirect to user's page
    end
end

我正在使用我自己的身份验证系统,这非常简单:我存储用户的 ID + cookie 中密码的哈希值。

当我不需要某些验证时,如何关闭它?

I have a simple User class with the following validation of name uniqueness:

class User < ActiveRecord::Base

  validates :name, :uniqueness => true,

It works great when a new user is created. However, when I check the login form, the user enters his name, and the system says it's already taken which doesn't make any sense.

So I implemented a separate valid_login? method, however I can't turn that unqueness check there:

def valid_login?
  validates :name, :uniqueness => false # doesn't work
end

This is my controller's code:

def login
    return unless request.post?

    @user = User.new(params[:user])
    if @user.valid_login?
      # Redirect to user's page
    end
end

I'm using my own authentication system which is quite simple: I store user's ID + password's hash in the cookies.

How can I turn of certain validation when I don't need it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

笙痞 2024-10-06 03:34:47

我用 :if/:unless 参数解决了这个问题。

I solved this problem with the :if/:unless parameters.

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