added inbox ajax thing
This commit is contained in:
parent
0226cff3e4
commit
1702c2fca4
|
@ -33,7 +33,41 @@ $(document).on "click", "button[name=qb-ask]", ->
|
||||||
$("div#question-box").hide()
|
$("div#question-box").hide()
|
||||||
$("div#question-box-promote").show()
|
$("div#question-box-promote").show()
|
||||||
$.snackbar # allahu snackbar
|
$.snackbar # allahu snackbar
|
||||||
content: "Successfully asked question"
|
content: data.message
|
||||||
|
style: "snackbar"
|
||||||
|
timeout: 5000
|
||||||
|
else
|
||||||
|
console.log data, status, jqxhr
|
||||||
|
$.snackbar # allahu snackbar
|
||||||
|
content: "An error occurred, a developer should check the console for details"
|
||||||
|
style: "snackbar"
|
||||||
|
timeout: 5000
|
||||||
|
error: (jqxhr, status, error) ->
|
||||||
|
console.log jqxhr, status, error
|
||||||
|
$.snackbar # allahu snackbar
|
||||||
|
content: "An error occurred, a developer should check the console for details"
|
||||||
|
style: "snackbar"
|
||||||
|
timeout: 5000
|
||||||
|
complete: (jqxhr, status) ->
|
||||||
|
btn.button "reset"
|
||||||
|
$("textarea[name=qb-question]").removeAttr "readonly"
|
||||||
|
|
||||||
|
$(document).on "click", "button[name=ib-answer]", ->
|
||||||
|
btn = $(this)
|
||||||
|
btn.button "loading"
|
||||||
|
iid = $("input[name=ib-id]").val()
|
||||||
|
$.ajax
|
||||||
|
url: '/ajax/inbox' # TODO: find a way to use rake routes instead of hardcoding them here
|
||||||
|
type: 'POST'
|
||||||
|
data:
|
||||||
|
id: iid
|
||||||
|
answer: $("textarea[name=ib-answer]").val()
|
||||||
|
success: (data, status, jqxhr) ->
|
||||||
|
if data.success
|
||||||
|
$("div#inbox-box[data-id=#{iid}]").val ''
|
||||||
|
$("div#inbox-box").slideUp()
|
||||||
|
$.snackbar # allahu snackbar
|
||||||
|
content: data.message
|
||||||
style: "snackbar"
|
style: "snackbar"
|
||||||
timeout: 5000
|
timeout: 5000
|
||||||
else
|
else
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
class Ajax::InboxController < ApplicationController
|
||||||
|
def destroy
|
||||||
|
params.require :id
|
||||||
|
params.require :answer
|
||||||
|
|
||||||
|
question = Question.create!(content: params[:question],
|
||||||
|
author_is_anonymous: params[:anonymousQuestion],
|
||||||
|
user: current_user)
|
||||||
|
|
||||||
|
unless current_user.nil?
|
||||||
|
current_user.increment! :answered_count
|
||||||
|
end
|
||||||
|
|
||||||
|
inbox = Inbox.find(params[:id]).first
|
||||||
|
|
||||||
|
unless current_user.id == Inbox.user_id
|
||||||
|
@status = :fail
|
||||||
|
@message = "question not in your inbox"
|
||||||
|
@success = false
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
answer = Answer.create(content: params[:answer],
|
||||||
|
user: current_user,
|
||||||
|
question: inbox.question)
|
||||||
|
|
||||||
|
Inbox.destroy inbox.id
|
||||||
|
|
||||||
|
@status = :okay
|
||||||
|
@message = "Successfully answered question."
|
||||||
|
@success = true
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1 @@
|
||||||
|
json.partial! 'ajax/shared/status'
|
|
@ -25,6 +25,7 @@ Rails.application.routes.draw do
|
||||||
|
|
||||||
namespace :ajax do
|
namespace :ajax do
|
||||||
match '/ask', to: 'question#create', via: :post, as: :ask
|
match '/ask', to: 'question#create', via: :post, as: :ask
|
||||||
|
match '/answer', to: 'inbox#destroy', via: :post, as: :answer
|
||||||
end
|
end
|
||||||
|
|
||||||
match '/inbox', to: 'inbox#show', via: 'get'
|
match '/inbox', to: 'inbox#show', via: 'get'
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class Ajax::InboxControllerTest < ActionController::TestCase
|
||||||
|
# test "the truth" do
|
||||||
|
# assert true
|
||||||
|
# end
|
||||||
|
end
|
|
@ -0,0 +1,4 @@
|
||||||
|
require 'test_helper'
|
||||||
|
|
||||||
|
class Ajax::InboxHelperTest < ActionView::TestCase
|
||||||
|
end
|
Loading…
Reference in New Issue