diff --git a/app/assets/javascripts/inbox.coffee b/app/assets/javascripts/inbox.coffee index 14c835e2..6c469012 100644 --- a/app/assets/javascripts/inbox.coffee +++ b/app/assets/javascripts/inbox.coffee @@ -71,6 +71,7 @@ $(document).on "click", "button[name=ib-answer]", -> id: iid answer: $("textarea[name=ib-answer][data-id=#{iid}]").val() share: JSON.stringify shareTo + inbox: true success: (data, status, jqxhr) -> if data.success $("div.inbox-box[data-id=#{iid}]").slideUp() diff --git a/app/controllers/ajax/answer_controller.rb b/app/controllers/ajax/answer_controller.rb index 197849b5..81842d37 100644 --- a/app/controllers/ajax/answer_controller.rb +++ b/app/controllers/ajax/answer_controller.rb @@ -1,4 +1,50 @@ class Ajax::AnswerController < ApplicationController + def create + params.require :id + params.require :answer + params.require :share + params.require :inbox + + inbox = (params[:inbox] == 'true') + + puts inbox + + if inbox + inbox_entry = Inbox.find(params[:id]) + + unless current_user == inbox_entry.user + @status = :fail + @message = "question not in your inbox" + @success = false + return + end + else + question = Question.find(params[:id]) + end + + answer = nil + + begin + answer = if inbox + inbox_entry.answer params[:answer], current_user + else + current_user.answer question, params[:answer] + end + rescue + @status = :err + @message = "An error occurred" + @success = false + return + end + + services = JSON.parse params[:share] + ShareWorker.perform_async(current_user.id, answer.id, services) + + @status = :okay + @message = "Successfully answered question." + @success = true + end + def destroy params.require :answer diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index 52b4ca8a..de146bcd 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -21,39 +21,6 @@ class Ajax::InboxController < ApplicationController inbox.update(new: false) end - def destroy - params.require :id - params.require :answer - params.require :share - - inbox = Inbox.find(params[:id]) - - unless current_user == inbox.user - @status = :fail - @message = "question not in your inbox" - @success = false - return - end - - answer = nil - - begin - answer = inbox.answer params[:answer], current_user - rescue - @status = :err - @message = "An error occurred" - @success = false - return - end - - services = JSON.parse params[:share] - ShareWorker.perform_async(current_user.id, answer.id, services) - - @status = :okay - @message = "Successfully answered question." - @success = true - end - def remove params.require :id diff --git a/app/views/ajax/inbox/destroy.json.jbuilder b/app/views/ajax/answer/create.json.jbuilder similarity index 100% rename from app/views/ajax/inbox/destroy.json.jbuilder rename to app/views/ajax/answer/create.json.jbuilder diff --git a/config/routes.rb b/config/routes.rb index 49f115f0..8180d9c2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -59,10 +59,10 @@ 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 '/generate_question', to: 'inbox#create', via: :post, as: :generate_question match '/delete_inbox', to: 'inbox#remove', via: :post, as: :delete_inbox match '/delete_all_inbox', to: 'inbox#remove_all', via: :post, as: :delete_all_inbox + match '/answer', to: 'answer#create', via: :post, as: :answer match '/destroy_answer', to: 'answer#destroy', via: :post, as: :destroy_answer match '/create_friend', to: 'friend#create', via: :post, as: :create_friend match '/destroy_friend', to: 'friend#destroy', via: :post, as: :destroy_friend