From 1bc6fb5458b21397b29fe62ad741fa772286f405 Mon Sep 17 00:00:00 2001 From: Osmond Oscar Date: Thu, 18 Feb 2016 14:21:58 +0100 Subject: [PATCH] Merge scope contraints for comments and votes routes --- app/controllers/votes_controller.rb | 6 +++--- config/routes.rb | 7 ++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index 918b852..6643c15 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -1,20 +1,20 @@ class VotesController < ApplicationController def upvote resource_to_upvote = vote_params[:resource_name].singularize.camelize.constantize - vote = Vote.act_on_vote('plus', resource_to_upvote, vote_params[:id], current_user) + vote = Vote.act_on_vote('plus', resource_to_upvote, vote_params[:resource_id], current_user) render json: { response: vote }, status: 200 unless vote.nil? render json: { error: "Invalid vote!" }, status: 403 if vote.nil? end def downvote resource_to_upvote = vote_params[:resource_name].singularize.camelize.constantize - vote = Vote.act_on_vote('minus', resource_to_upvote, vote_params[:id], current_user) + vote = Vote.act_on_vote('minus', resource_to_upvote, vote_params[:resource_id], current_user) render json: { response: vote }, status: 200 unless vote.nil? render json: { error: "Invalid vote!" }, status: 403 if vote.nil? end private def vote_params - params.permit(:resource_name, :id) + params.permit(:resource_name, :resource_id) end end diff --git a/config/routes.rb b/config/routes.rb index 814c997..8347f19 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,8 @@ Rails.application.routes.draw do - scope '/:resource_name/:id', constraints: { resource_name: /(questions|answers|comments)/ } do + scope '/:resource_name/:resource_id', constraints: { resource_name: /(questions|answers|comments)/ } do post '/upvote', to: 'votes#upvote' post '/downvote', to: 'votes#downvote' - end - - scope '/:resource_name/:resource_id', constraints: { resource_name: /(questions|answers)/} do - resources :comments, except: [:new, :edit] + resources :comments, except: [:new, :edit], resource_name: /(?!comments).*/ end post '/validate_token', to: 'tokens#validate'