# Validates\_peresence\_of

**URL:** <https://www.ruby-forum.com/t/validates-peresence-of/98098>\
**Category:** Rails\
**Created:** [July 12, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/validates-peresence-of/98098 "2006-07-12T16:05:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 12, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/validates-peresence-of/98098/1 "2006-07-12T16:05:44Z")

</div>

Is there an expert validater that can point me in the right direction?  
I’ve looked for extended documentation on ‘validates\_presence\_of’ but i  
can’t seem to find any.

I’m trying to make sure that i don’t enter a nil object in my database,  
so i’ve added validate\_presence\_of to my Record Model.

i.e. --\>  
class Record \< ActiveRecord::Base  
validate\_presence\_of  
end

I’m coming up with errors when trying this. Do i need to create a  
function to handle the situation when i try leaving a field blank? Does  
any one know of a good place to find detailed info on the validates  
methods (Please don’t point me to the api, which has nothing worth while  
there.

Thanks,  
Yng

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 12, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/validates-peresence-of/98098/2 "2006-07-12T16:33:44Z")

</div>

Yngwie wrote:

> Is there an expert validater that can point me in the right direction?  
> I’ve looked for extended documentation on ‘validates\_presence\_of’ but i  
> can’t seem to find any.
> 
> I’m trying to make sure that i don’t enter a nil object in my database,  
> so i’ve added validate\_presence\_of to my Record Model.
> 
> i.e. --\>  
> class Record \< ActiveRecord::Base  
> validate\_presence\_of  
> end
> 
> I’m coming up with errors when trying this. Do i need to create a  
> function to handle the situation when i try leaving a field blank? Does  
> any one know of a good place to find detailed info on the validates  
> methods (Please don’t point me to the api, which has nothing worth while  
> there.
> 
> Thanks,  
> Yng

A little more info w/ details ==\>  
I have an edit .rhtml page which has the user enter ‘record’ data  
fields. When I go to update these fields, i’m directed to the Update  
controller which looks like

def update  
@record.Record.find(params[‘id’])  
if( @record.update\_attributes(params[‘record’])  
redirect\_to :action =\> ‘view’  
else  
redirect\_to :action =\> ‘edit’  
end  
end

So when i leave a field, that i’m validataing presence of, blank the  
record doesn’t save and it goes directly to the else statement  
(redirect\_to :action =\> ‘edit’) How do I get the page to reload and  
prompt the user to fill in the blank fields?

Thanks in advance,  
Yng

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 12, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/validates-peresence-of/98098/3 "2006-07-12T16:34:01Z")

</div>

Yngwie wrote:

> Is there an expert validater that can point me in the right direction?  
> I’ve looked for extended documentation on ‘validates\_presence\_of’ but i  
> can’t seem to find any.
> 
> I’m trying to make sure that i don’t enter a nil object in my database,  
> so i’ve added validate\_presence\_of to my Record Model.
> 
> i.e. --\>  
> class Record \< ActiveRecord::Base  
> validate\_presence\_of  
> end
> 
> I’m coming up with errors when trying this. Do i need to create a  
> function to handle the situation when i try leaving a field blank? Does  
> any one know of a good place to find detailed info on the validates  
> methods (Please don’t point me to the api, which has nothing worth while  
> there.
> 
> Thanks,  
> Yng

A little more info ==\>  
I have an edit .rhtml page which has the user enter ‘record’ data  
fields. When I go to update these fields, i’m directed to the Update  
controller which looks like

def update  
@record.Record.find(params[‘id’])  
if( @record.update\_attributes(params[‘record’])  
redirect\_to :action =\> ‘view’  
else  
redirect\_to :action =\> ‘edit’  
end  
end

So when i leave a field, that i’m validataing presence of, blank the  
record doesn’t save and it goes directly to the else statement  
(redirect\_to :action =\> ‘edit’) How do I get the page to reload and  
prompt the user to fill in the blank fields?

Thanks in advance,  
Yng

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 12, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/validates-peresence-of/98098/4 "2006-07-12T16:39:34Z")

</div>

“Yngwie” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

Hi yng

I’m not sure what you mean by “nothing worthwhile” in the api docs. At  
least  
for validations, the documentation is very good:  
[http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html](http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html)

> i.e. →   
> class Record \< ActiveRecord::Base  
> validate\_presence\_of  
> end

it should be validates\_presence\_of, (you’re missing an s on ‘validate’)  
and  
you need to specify which attributes you are trying to validate.

Here’s an example from the api docs:

class Person \< ActiveRecord::Base  
validates\_presence\_of :first\_name  
end

hth  
alan

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 12, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/validates-peresence-of/98098/5 "2006-07-12T16:46:42Z")

</div>

> you need to specify which attributes you are trying to validate.
> 
> Here’s an example from the api docs:
> 
> class Person \< ActiveRecord::Base  
> validates\_presence\_of :first\_name  
> end
> 
> hth  
> alan

Hey Alan,  
thanks for the timely response,

I misspelled validates on this blog, but it’s was correctly spelled in  
my record model. When i said the api wasn’t worthwhile, i meant that  
the simple example they have doesn’t do me any good. I try to follow  
the model, and ‘validates\_presence\_of’ just doesn’t allow me to save the  
record. I need the user to be prompted to enter in a valid field. any  
insight?

Thxs,  
Yngwie

---

<div class="post-metadata">

**Author:** ![Yngwie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8dc957/32.png) [@Yngwie](https://www.ruby-forum.com/u/Yngwie)\
**Post date:** [July 12, 2006, 7:44pm UTC](https://www.ruby-forum.com/t/validates-peresence-of/98098/6 "2006-07-12T19:44:25Z")

</div>

You may be asking how to display the validation errors to the user.  
In that case, you want to use error\_messages\_for \<http://  
[api.rubyonrails.com/classes/ActionView/Helpers/](http://api.rubyonrails.com/classes/ActionView/Helpers/)  
ActiveRecordHelper.html#M000460\> in the view.

The easiest way to learn this is to use “/script/generate scaffold  
Test” and look at the code generated. It has nice examples of using  
validations and displaying errors to users.

```
        - dan

```

–  
Dan K. [mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.dankohn.com/](http://www.dankohn.com/)tel:+1-415-233-1000
