Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.