Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save equivalent/cb5666f62b6e377f3f15853ec5eb49e5 to your computer and use it in GitHub Desktop.
Save equivalent/cb5666f62b6e377f3f15853ec5eb49e5 to your computer and use it in GitHub Desktop.
Request model with validation on error model
require 'active_model'
class RequestModel
include ActiveModel::Validations
attr_reader :params, :ero
def initialize(params, ero)
@params = params
@ero = ero
end
validates :title,
length: { maximum: 255 },
presence: true
def title
params[:title]
end
def valid?
result = super
unless result
ero << errors
end
result
end
end
puts 'without error'
ero = []
puts RequestModel.new({title: 'a'}, ero).valid?
puts ero.inspect
puts 'with error'
ero = []
x = RequestModel.new({x: 'a'},ero)
puts x.valid?
puts ero.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment