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 Junior <rails-mailing-l...@andreas-
s.net> wrote:> Hi guys,
>
> I am newbie in Rails world and I am using the Agile Web Development 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)
> <h1>Products#index</h1>
> <%= error_messages_for :product %>
> <% form_for :product,:url => {:action => :create} do |form| %>
> Name: <%= form.text_field :name, :size => 30 %><br />
> <%= 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.
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---