jeudi 23 avril 2015

class Validator in Ruby on rails

I am reading the rails guides. I am trying create a class Validator but I get the following error message in console

uninitialized constant Product::GoodnessValidator

model product.rb

class Product < ActiveRecord::Base
   validates_with GoodnessValidator

end

class GoodnessValidator < ActiveModel::Validator
  def validate(record)
    if record.amount == 12
      record.errors[:base] << "The amount musn't be 12"
    end
  end
end

Aucun commentaire:

Enregistrer un commentaire