added (ajax) answer controller

This commit is contained in:
nilsding 2014-11-26 17:05:46 +01:00
parent 96dbb77f7c
commit 8dff4462cf
3 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,37 @@
class Ajax::AnswerController < ApplicationController
def destroy
params.require :answer
unless current_user.nil?
current_user.increment! :asked_count unless params[:anonymousQuestion] == 'true'
end
answer = Answer.find(params[:answer])
unless answer.user == current_user || privileged?
@status = :nopriv
@message = "check yuor privlegs"
@success = false
return
end
answer.user.decrement! :answered_count
Inbox.create!(user: answer.user, question: answer.question, new: true)
answer.destroy
@status = :okay
@message = "Successfully deleted answer."
@success = true
end
private
# TODO:
def privileged?
if current_user && current_user.admin?
true
else
false
end
end
end

View File

@ -0,0 +1 @@
json.partial! 'ajax/shared/status'

View File

@ -26,6 +26,7 @@ Rails.application.routes.draw do
namespace :ajax do
match '/ask', to: 'question#create', via: :post, as: :ask
match '/answer', to: 'inbox#destroy', via: :post, as: :answer
match '/destroy_answer', to: 'answer#destroy', via: :post, as: :destroy_answer
end
match '/inbox', to: 'inbox#show', via: 'get'