Last active
April 26, 2017 04:29
-
-
Save edwinlab/e13e8bc234fbddc0962d7e0d1253b350 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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