0

I am having an issue while saving a user object in the database. I have two fields in the model (email and password) which are not allowed to be null in the database itself. Plus I have added validation in the model like

  validates_presence_of :email, :message => "must be provided"
  validates_presence_of :password, :message => "must be provided"

Now when I try to save the model from the create method of the controller, it invalidates the data and renders the new action again. However I have multiple error messages for each field

Email can't be blank
Email must be provided
Password can't be blank
Password must be provided

I don't need multiple error messages for the same one. How can I eliminate this?

rajan sthapit
  • 4,194
  • 10
  • 42
  • 66
  • You have two lots of validation happening by the looks of it. Can you post your model code? Does your model inherit directly from `ActiveRecord::Base` or have you interjected a class between your model class and it? If so, do you have validations in the intermediate class. Basically, we need some more to go on. :) – Ben Nov 15 '11 at 22:49

2 Answers2

1

Looks like you are validating in two different places. You have to figure it out the places...

If you are doing two different validation for a field and want to display one error message on a field at a time, you can do the following,

validates_presence_of   :email, :message => "must be provided"
validates_uniqueness_of :email, :message => "must be unique", 
     :if => lambda { |a| a.errors.on(:email).blank? }
nkm
  • 5,844
  • 2
  • 24
  • 38
0

Looks like you are rendering errors twice. Check all your views, they can also be inherited.

Bob
  • 2,081
  • 18
  • 25
  • Hmm, the errors are different though. It looks more like there are two sets of validations going on - rendering again in Rails won't cause a re-validation anyway. – Ben Nov 15 '11 at 22:44