Module | ActiveResource::Validations |
In: |
lib/active_resource/validations.rb
|
Module to support validation and errors with Active Resource objects. The module overrides Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned in the web service response. The module also adds an errors collection that mimics the interface of the errors provided by ActiveRecord::Errors.
Consider a Person resource on the server requiring both a first_name and a last_name with a validates_presence_of :first_name, :last_name declaration in the model:
person = Person.new(:first_name => "Jim", :last_name => "") person.save # => false (server returns an HTTP 422 status code and errors) person.valid? # => false person.errors.empty? # => false person.errors.count # => 1 person.errors.full_messages # => ["Last name can't be empty"] person.errors.on(:last_name) # => "can't be empty" person.last_name = "Halpert" person.save # => true (and person is now saved to the remote service)