# Validation doesn't work (newbie in Rails)

**URL:** <https://www.ruby-forum.com/t/validation-doesnt-work-newbie-in-rails/129163>\
**Category:** Rails\
**Created:** [February 15, 2008, 12:27am UTC](https://www.ruby-forum.com/t/validation-doesnt-work-newbie-in-rails/129163 "2008-02-15T00:27:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrjuniorsp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jrjuniorsp](https://www.ruby-forum.com/u/jrjuniorsp)\
**Post date:** [February 15, 2008, 12:27am UTC](https://www.ruby-forum.com/t/validation-doesnt-work-newbie-in-rails/129163/1 "2008-02-15T00:27:09Z")

</div>

Hi guys,

I am newbie in Rails world and I am using the Agile Web D. with  
Rails book to learn Rails.

I am trying to create a simple example using validation, actually only  
one field on the User Interface and this field uses the  
validates\_presence\_of validation.

Although this is a simple example, it doesn’t work for me. Below  
following the source code of the VIEW, CONTROLLER, MODEL.

View (index.html.erb)

# Products#index
\<%= error\_messages\_for :product %\> \<% form\_for :product,:url =\> {:action =\> :create} do |form| %\> Name: \<%= form.text\_field :name, :size =\> 30 %\>  
 \<%= submit\_tag %\> \<% end %\>

Controller (products\_controller.rb)  
class ProductsController \< ApplicationController

def index  
end

def create  
@product = Products.new(params[:product])  
end  
end

Model (products.rb)  
class Products \< ActiveRecord::Base  
validates\_presence\_of :name, :message =\> “This is a required field”  
end

* * *

As you can see, it is very simple. However it is not working properly.  
If you click “Submit” on index page, even if the “name” field is empty,  
the create.html.erb is called.

Does anyone could help me?

Thanks in advice.

---

<div class="post-metadata">

**Author:** ![jrjuniorsp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bb73d2/32.png) [@jrjuniorsp](https://www.ruby-forum.com/u/jrjuniorsp)\
**Post date:** [February 15, 2008, 12:45am UTC](https://www.ruby-forum.com/t/validation-doesnt-work-newbie-in-rails/129163/2 "2008-02-15T00:45:25Z")

</div>

The only problem I can see off hand is that you might need to change  
your create action:

def create  
@product = Products.new(params[:product])  
if @product.save  
redirect\_to :action =\> :list  
else  
render :action =\> :index  
end  
end

On Feb 15, 12:27 pm, Jair Rillo J. \<rails-mailing-l…@andreas-
