Topic: Adding to the errors hash with "add" or "add_to_base"method

Controller:

def verify_register
  @user = User.new(params[:user])
  if User.find_by_nick(@user.nick)
    errors.add_to_base("That Screen Name is already taken")
    render :controller => "user", :action => "register"
  elsif User.find_by_email(@user.email)
    errors.add(email,"There is already an account registered with that email address")
    render :controller => "user", :action => "register"
  else
    if @user.save
      session[:uid] = @user.id
      flash[:notice] = "Hello, #{@user.first_name}. Welcome to RaceBucket."
      redirect_to :controller => "user", :action => "mybucket"
    else
      render :controller => "user", :action => "register"
    end
  end
end

Getting the following error:
undefined local variable or method `errors' for #<UserController:0x68a43a4>

What am I doing wrong?

Re: Adding to the errors hash with "add" or "add_to_base"method

Validation should go in the model, not the controller.

class User < ActiveRecord::Base
  validates_uniqueness_of :nick
  validates_uniqueness_of :email
end

If you want to customize the validation more, you can create a "validate" method in the model and add errors there - this will be run automatically before saving.

Railscasts - Free Ruby on Rails Screencasts

Re: Adding to the errors hash with "add" or "add_to_base"method

Thanks Ryan. Brainfart. I should have picked up on that.