views:

62

answers:

2

Hi guys , i am currently trying to display my errors that i added to user object inside the controller simultaneously with my validation inside the model.

like if maybe i have an error in my controller it display it immediately then return and without displaying the errors inside the model, i know that it has to return if my errors are finished counting and display all of them not one by one.

i even wrote a validation method that should check the validations inside the model and save them to my object then the display error method can display all the errors including the one's that was found in the model.

my controllers methods are like this

 def info

   if @user.firstname != "" && @user.lastname != "" && @user.id_number != "" && @user.email != ""
     @user.errors.add_to_base("Password can't be blank")
   end

 end



 def validations()
   @errors = User.check_validations
 end 

 def display(template_to_render)
   if @user.errors.count >= 1
     render :action => template_to_render
   end   
 end

Then my method in the model is as follows

def  self.check_validations
  validates_presence_of :firstname, :lastname, :date_of_birth, :if => Proc.new { |o|   o.force_update? || o.profile_confirmed? }
end

then i want to add all the errors of validations method to the @user.errors.to_base errors and display them all.

so i was wondering if there is any method maybe that i can use to check the method inside the model and add all those errors to the @user object before it can display on the view.

+1  A: 

A couple things.

  1. All validations should go in the model, you shouldn't call errors.add in the controller.

  2. The validates_presence_of is a class method which defines the validations which should take place. The validation does not happen at that exact point. Therefore you can not use this on a per-request basis.

If you want to do just one validation and then validate the rest of the model at a later time, try this.

class User < ActiveRecord::Base
  validate :check_password
  validates_presence_of :firstname, :lastname, :date_of_birth, :if => Proc.new { |o|   o.force_update? || o.profile_confirmed? }

  def check_password
    if firstname != "" && lastname != "" && id_number != "" && email != ""
      errors.add_to_base("Password can't be blank")
    end
  end
end

You can then call check_password directly to validate only that when needed.

def info
  @user.check_password
end

def validations
  @user.valid? # triggers validations
  @errors = @user.errors
end 

def display(template_to_render)
  if @user.errors.count >= 1
    render :action => template_to_render
  end   
end

I'm assuming those methods are all called in a single request and they aren't each separate controller actions.

ryanb
A: 

What you are doing in "self.check_validations" is adding a validation every time your controller runs. After 1000 requests you will have 1000 validations added to the model and a crashed app (probably).

Look up "conditional validations" in Rails docs, this will explain how to achieve what you want.

You can also investigate .save! and .create! methods where you get exceptions if the model is invalid - this allows you to alter the control flow in a more explicit way

Julik