rails/activemodel/examples/validations.rb
Vijay Ubuntu 27576df810 adding load_path to include active_model in the load path of example
similar to activerecord/examples/simple.rb
2013-10-09 01:52:55 +05:30

31 lines
551 B
Ruby

require File.expand_path('../../../load_paths', __FILE__)
require 'active_model'
class Person
include ActiveModel::Conversion
include ActiveModel::Validations
validates :name, presence: true
attr_accessor :name
def initialize(attributes = {})
@name = attributes[:name]
end
def persist
@persisted = true
end
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