Rails:使用用户注册/登录验证
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我用 :if/:unless 参数解决了这个问题。
I solved this problem with the :if/:unless parameters.