Gee… it sure would be awesome if ActiveResource actually allowed us to do client-side validation of our ActiveResource objects like the ActiveResource::Base documentation promises


To install, add this to your environment.rb:

config.gem 'wallet', :version => '0.0.2', :lib => 'wallet', :source => ''

Next, go to the root of your project in a shell and run this command (you may need to use “sudo” depending on your environment):

sh# rake gems:install


You can now create a protected “validate” method in your ActiveResource::Base-derived class:

class Book < ActiveResource::Base = 'http://myservice.domain'

  def validate
    errors.add(:author, 'is required') if (author.blank? rescue true)

Now, in your code, you can do the following:

irb> b =
irb> b.valid?
  ==> false
irb> = ''
irb> b.valid?
  ==> false
irb> = 'Iain M. Banks'
irb> b.valid?
  ==> true

When you call the valid? method on your active resource object, it will do client side validation. It will not send a request to the service provider for validation.