Report abuse

sample from http://api.rubyonrails.org/classes/ActiveResource/Validations.html

class Person < ActiveResource::Base
   self.site = "http://www.fake.com/" # doesn't matter because an invalid object won't even attempt to save
   def validate
     errors.add_on_empty %w( first_name last_name )
     errors.add("phone_number", "has invalid format") unless phone_number =~ /[0-9]*/
   end
end

Running the example code

person = Person.new("first_name" => "Jim", "phone_number" => "I will not tell you.")
#"I will not tell you.", "first_name"=>"Jim"}, @prefix_options={}>
p.save 
# => ActiveResource::MethodNotAllowed: Failed with 405 Method not allowed

A method with undefined variables

# You can ever but in broken code into the validate method. It's never called
class Person < ActiveResource::Base
   self.site = "http://www.fake.com/" # doesn't matter because an invalid object won't even attempt to save
   def validate
     a
     b
     c
   end
end

Running the code

person = Person.new("first_name" => "Jim", "phone_number" => "I will not tell you.")
#=> "I will not tell you.", "first_name"=>"Jim"}, @prefix_options={}>
p.save 
# => ActiveResource::MethodNotAllowed: Failed with 405 Method not allowed

You can even manually add in errors and the object will still attempt a remote save

person.errors.add(:base, "I am invalid and won't even try bother the server")
# => ["I am invalid and won't even try to bother the server"]
p.valid?
# => false
p.save
# => ActiveResource::MethodNotAllowed: Failed with 405 Method not allowed