validates_presence_of :parent_id, :message => “You must specify a parent
for this keyword.”, :on => :create
replied too early.
your solution does not work if I want to do several verifications on
It also does not answer why I was getting the error.
So my problem remains.
This should work, but somehow it doesn’t:
validates_presence_of :parent_id, :message => “message.”
more methods here
undefined method `validates_presence_of’ for #Keyword:0xb774aba0
validates_presence_of is designed to be used on its own, not in a
method–it’s a class method rather than an instance method. If you want
to make your own custom “validate_on_create”, then use Ruby to define
the conditions yourself.
errors.add(:parent_id, “cannot be blank.”)
if other_condition == true
errors.add_to_base(“You can test as many conditions as you want in
That’s how the custom validation functions work–you test the conditions
and add to “errors” to flag a condition as invalid.
Alternatively, the way described above doesn’t actually keep you from
supplying multiple conditions on create:
validates_presence_of :parent_id, :name, :price, :message => “cannot
be blank.”, :on => :create
validates_numericality_of :price, :on => :create
validates_acceptance_of :terms_of_service, :on => :create
…and so on.
Check the Agile Web D. With Rails book for more examples, it’s
an invaluable resource. The Rails API documentation will have the
specifics on how to use “errors.add” and “errors.add_to_base”.
Hope this helps,