lundi 27 avril 2015

Pass parameters to method of validation customized

I am a newbie in Rails. I am trying pass a value to a method of validation customized, but I get the following error

class Person < ActiveRecord::Base
  attr_accessible :first_name, :last_name, :age

  validate :something, 56

  def something(value=67)
   errors.add(:first_name, "cannot contain the characters !@#%*()_-+= # #{value}") 
  end
end

error

1.9.3-p194 :002 > p=Person.new
 => #<Person id: nil, first_name: nil, last_name: nil, age: nil, is_admin: nil, created_at: nil, updated_at: nil> 
1.9.3-p194 :003 > p.save
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
NoMethodError: undefined method `validate' for 56:Fixnum
    from /home/fernando/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/callbacks.rb:310:in `_callback_before_13'

Aucun commentaire:

Enregistrer un commentaire