开发者

Rails: using validation for user register/login

开发者 https://www.devze.com 2023-01-21 18:28 出处:网络
I have a simple User class with the following validation of name uniqueness: class User < ActiveRecord::Base

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 implement开发者_开发百科ed 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消