Skip to content

Instantly share code, notes, and snippets.

@bernd
Created August 23, 2012 09:27
Show Gist options
  • Save bernd/3434627 to your computer and use it in GitHub Desktop.
Save bernd/3434627 to your computer and use it in GitHub Desktop.
Validate hash via ActiveModel::Validations
require 'bundler/setup'
require 'active_model'
class ParamValidation
include ActiveModel::Validations
validates :page, :format => /\d+/
attr_accessor :page
def initialize(params)
self.page = params[:page]
end
end
params = {:page => 'abc'}
v = ParamValidation.new(params)
puts v.valid?.inspect
puts v.errors.full_messages.inspect
params = {:page => 123}
v = ParamValidation.new(params)
puts v.valid?.inspect
puts v.errors.full_messages.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment