Class: Rapscallion::ValidationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/rapscallion/validations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/rapscallion/validations_controller.rb', line 10

def create
	klass = params[:klass].camelize.constantize

	if params[:existing_record].present?
		item = klass.find(params[:existing_record])
	else
		item = klass.new
	end

	field = params[:attr]
	value = params[:field_val]
	item.send("#{field}=", value)
	item.valid?
	errors = item.errors[field].uniq

	if !field.match(/_confirmation/).nil?
		field_to_confirm_against = field.gsub('_confirmation', '')
		x = params[field_to_confirm_against.to_sym]
		
		errors = ["doesn't match confirmation"]  unless value == x
	end

	render json: errors.to_json
	
	rescue => e
		logger.info "ERROR: #{e}"
end