Please help, I am really at a loss at how validation is supposed to work
in rails.
Model contains
acts_as_tree
I want to force my NEW objects to have a parent
I do NOT want existing object to have a parent
so I only want to have parent_id on create, NOT on update.
I am trying this:
def validate_on_create
validates_presence_of :parent_id, :message => "You must specify a
parent for this keyword."
end
and I get:
undefined method `validates_presence_of’ for #Keyword:0xb774aba0
The only time it works is if I remove validate_on_create, but then I can
no longer update existing entries that do not have a parent!
this is driving me mad.
please help.
many thanks
Isabelle
Isabelle wrote:
Please help, I am really at a loss at how validation is supposed to work
in rails.
Model contains
acts_as_tree
I want to force my NEW objects to have a parent
I do NOT want existing object to have a parent
so I only want to have parent_id on create, NOT on update.
I am trying this:
def validate_on_create
validates_presence_of :parent_id, :message => "You must specify a
parent for this keyword."
end
and I get:
undefined method `validates_presence_of’ for #Keyword:0xb774aba0
The only time it works is if I remove validate_on_create, but then I can
no longer update existing entries that do not have a parent!
this is driving me mad.
please help.
many thanks
Isabelle
Hi,
maybe try
validates_presence_of :parent_id, :message => “You must specify a parent
for this keyword.”, :on => :create
?
Agnieszka
Isabelle wrote:
validates_presence_of :parent_id, :message => “You must specify a parent
for this keyword.”, :on => :create
of course!
many thanks.
replied too early.
your solution does not work if I want to do several verifications on
create.
It also does not answer why I was getting the error.
So my problem remains.
This should work, but somehow it doesn’t:
def validate_on_create
validates_presence_of :parent_id, :message => “message.”
more methods here
end
returns:
undefined method `validates_presence_of’ for #Keyword:0xb774aba0
Hi Isabelle,
Isabelle wrote:
Just to check assumptions, where does the code below live? The model,
controller, or somewhere else?
Best regards,
Bill
thanks Jeff, now I see how it works.
Isabelle
Isabelle wrote:
Isabelle wrote:
validates_presence_of :parent_id, :message => “You must specify a parent
for this keyword.”, :on => :create
of course!
many thanks.
replied too early.
your solution does not work if I want to do several verifications on
create.
It also does not answer why I was getting the error.
So my problem remains.
This should work, but somehow it doesn’t:
def validate_on_create
validates_presence_of :parent_id, :message => “message.”
more methods here
end
returns:
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.
def validate_on_create
if parent_id.blank?
errors.add(:parent_id, “cannot be blank.”)
end
if other_condition == true
errors.add_to_base(“You can test as many conditions as you want in
this method.”)
end
end
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,
Jeff C.man