I don't understand the validate method inside the model objet, when
using the fields are always empty ?
class Personne < ActiveRecord::Base
set_table_name 'personnes'
has_many :emails
protected
def validate
if nom = ""
errors.add( "nom", "Le nom ne peut pas etre vide" )
end
if prenom = ""
errors.add( "prenom", "Le prenom ne peut pas etre vide" )
end
end
def validate
if nom = ""
errors.add( "nom", "Le nom ne peut pas etre vide" )
end
if prenom = ""
errors.add( "prenom", "Le prenom ne peut pas etre vide" )
end
end
You are setting nom equal to blank by using nom = "". Instead, write
nom == "" to compare nom to blank. Of course you'll do the same with
prenom.
you can use the method "validates_presence_of" to validate the fields
in blank:
class Personne < ActiveRecord::Base
set_table_name 'personnes'
has_many :emails
validates_presence_of :nom, :message => "Le nom ne peut pas etre
vide"
validates_presence_of :prenom, :message => "Le prenom ne peut pas
etre vide"
end