Skip to content

Instantly share code, notes, and snippets.

@edwinlab
Last active April 26, 2017 04:29
Show Gist options
  • Select an option

  • Save edwinlab/e13e8bc234fbddc0962d7e0d1253b350 to your computer and use it in GitHub Desktop.

Select an option

Save edwinlab/e13e8bc234fbddc0962d7e0d1253b350 to your computer and use it in GitHub Desktop.
module Validate
class Activity
include ActiveModel::Validations
attr_accessor :latitude, :longitude
validates :latitude, presence: true, numericality: true
validates :longitude, presence: true, numericality: true
def initialize(params={})
@latitude = params[:latitude]
@longitude = params[:longitude]
ActionController::Parameters.new(params).permit(:latitude,:longitude)
end
end
end
class LocationsController < ApplicationController
before_action :validate_params
def index
end
rescue_from(ActionController::UnpermittedParameters) do |pme|
render json: { error: { unknown_parameters: pme.params } },
status: :bad_request
end
private
def validate_params
location = Validate::Location.new(params)
if !location.valid?
render json: { error: location.errors } and return
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment