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.