rails--rails/activemodel/examples/validations.rb

30 lines
492 B
Ruby
Raw Normal View History

2009-09-14 19:54:43 +00:00
require 'active_model'
class Person
2009-07-21 05:56:27 +00:00
include ActiveModel::Conversion
include ActiveModel::Validations
2009-07-21 05:56:27 +00:00
validates_presence_of :name
2009-07-21 05:56:27 +00:00
attr_accessor :name
2009-07-21 05:56:27 +00:00
def initialize(attributes = {})
@name = attributes[:name]
end
2009-07-21 05:56:27 +00:00
def persist
@persisted = true
end
2009-07-21 05:56:27 +00:00
def persisted?
@persisted
end
end
person1 = Person.new
p person1.valid? # => false
p person1.errors.messages # => {:name=>["can't be blank"]}
person2 = Person.new(:name => "matz")
p person2.valid? # => true