# Does not validate presence of entry fields

**URL:** <https://www.ruby-forum.com/t/does-not-validate-presence-of-entry-fields/150839>\
**Category:** Rails\
**Created:** [October 14, 2008, 2:01am UTC](https://www.ruby-forum.com/t/does-not-validate-presence-of-entry-fields/150839 "2008-10-14T02:01:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [October 14, 2008, 2:01am UTC](https://www.ruby-forum.com/t/does-not-validate-presence-of-entry-fields/150839/1 "2008-10-14T02:01:09Z")

</div>

Hi all

I wrote a small form and I am trying to check if the fields are  
populated.

My Model is:  
class Contact \< ActiveRecord::Base  
validates\_presence\_of :name, :email, :body  
validates\_length\_of :body, :maximum =\>2000  
end

-----Controller  
class ContactController \< ApplicationController  
def new  
@contact = Contact.new  
end  
def create  
@contact = Contact.new(params[:contact])  
@contact.save  
end  
end

------Viewers

New view

\<%= error\_messages\_for :contact %\>

\<% form\_for @contact, :url =\> { :action =\> ‘create’ }, :html =\> {  
:method =\> :post } do |f| %\>

Please send your message: 

Your N.:  
\<%= f.text\_field :name, :size =\> 25 %\>

Your email:  
 \<%= f.text\_field :email, :size =\> 25 %\>

Message:  
\<%= f.text\_area :body, :rows =\> 10, :cols =\> 30 %\>

\<%= submit\_tag 'Submit' %\>

\<% end %\>

Create view

## Thank you for your interest in my site

When the entry fields are not populated the forms shows the create view  
and does not show any error message. why?

thanks

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [October 14, 2008, 4:08am UTC](https://www.ruby-forum.com/t/does-not-validate-presence-of-entry-fields/150839/2 "2008-10-14T04:08:03Z")

</div>

> Your N.:  
> \<%= f.text\_field :name, :size =\> 25
> 
> Create view
> 
> ## Thank you for your interest in my site
> 
> When the entry fields are not populated the forms shows the create  
> view  
> and does not show any error message. why?

Because you aren’t doing anything in the create action to adjust the  
result if there is a failure. You want something more like this:

def create  
@contact = Contact.new(params[:contact])  
unless @contact.save  
render :action =\> ‘new’  
return  
end  
end

Or this which is pretty close to the default generated by Rails  
scaffolding.

def create  
@contact = Contact.new(params[:contact])

```
 if @contact.save
   flash[:notice] = 'Contact was successfully created.'
   redirect_to(contact_path(@contact))
 else
   render :action => "new"
 end

```

end

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [October 15, 2008, 3:33am UTC](https://www.ruby-forum.com/t/does-not-validate-presence-of-entry-fields/150839/3 "2008-10-15T03:33:08Z")

</div>

thanks!!!
