From ba7bd2239b23be83ab06450d9c29835848a2e93b Mon Sep 17 00:00:00 2001 From: nilsding Date: Tue, 11 Nov 2014 18:02:59 +0100 Subject: [PATCH] answering works now. --- .../javascripts/application.js.erb.coffee | 48 +++++++------------ app/controllers/ajax/inbox_controller.rb | 4 +- app/views/inbox/show.html.haml | 19 +++++--- 3 files changed, 33 insertions(+), 38 deletions(-) diff --git a/app/assets/javascripts/application.js.erb.coffee b/app/assets/javascripts/application.js.erb.coffee index a79c6106..aaf33e75 100644 --- a/app/assets/javascripts/application.js.erb.coffee +++ b/app/assets/javascripts/application.js.erb.coffee @@ -10,6 +10,12 @@ NProgress.configure showSpinner: false +showSnackbar = (text) -> + $.snackbar # allahu snackbar + content: data.message + style: "snackbar" + timeout: 5000 + $(document).on "click", "button[name=qb-ask]", -> btn = $(this) btn.button "loading" @@ -32,22 +38,13 @@ $(document).on "click", "button[name=qb-ask]", -> if promote $("div#question-box").hide() $("div#question-box-promote").show() - $.snackbar # allahu snackbar - content: data.message - style: "snackbar" - timeout: 5000 + showSnackbar data.message 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 + showSnackbar "An error occurred, a developer should check the console for details" 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 + showSnackbar "An error occurred, a developer should check the console for details" complete: (jqxhr, status) -> btn.button "reset" $("textarea[name=qb-question]").removeAttr "readonly" @@ -55,36 +52,27 @@ $(document).on "click", "button[name=qb-ask]", -> $(document).on "click", "button[name=ib-answer]", -> btn = $(this) btn.button "loading" - iid = $("input[name=ib-id]").val() + iid = btn[0].dataset.ibId + $("textarea[name=ib-answer][data-id=#{iid}]").attr "readonly", "readonly" $.ajax - url: '/ajax/inbox' # TODO: find a way to use rake routes instead of hardcoding them here + url: '/ajax/answer' # TODO: find a way to use rake routes instead of hardcoding them here type: 'POST' data: id: iid - answer: $("textarea[name=ib-answer]").val() + answer: $("textarea[name=ib-answer][data-id=#{iid}]").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" - timeout: 5000 + $("div.inbox-box[data-id=#{iid}]").slideUp() + showSnackbar data.message 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 + showSnackbar "An error occurred, a developer should check the console for details" 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 + showSnackbar "An error occurred, a developer should check the console for details" complete: (jqxhr, status) -> btn.button "reset" - $("textarea[name=qb-question]").removeAttr "readonly" + $("textarea[name=ib-answer][data-id=#{iid}]").removeAttr "readonly" $(document).on "click", "button#create-account", -> Turbolinks.visit "/sign_up" diff --git a/app/controllers/ajax/inbox_controller.rb b/app/controllers/ajax/inbox_controller.rb index 1fb0dd48..00541521 100644 --- a/app/controllers/ajax/inbox_controller.rb +++ b/app/controllers/ajax/inbox_controller.rb @@ -11,9 +11,9 @@ class Ajax::InboxController < ApplicationController current_user.increment! :answered_count end - inbox = Inbox.find(params[:id]).first + inbox = Inbox.find(params[:id]) - unless current_user.id == Inbox.user_id + unless current_user == inbox.user @status = :fail @message = "question not in your inbox" @success = false diff --git a/app/views/inbox/show.html.haml b/app/views/inbox/show.html.haml index c9e717d7..674becdc 100644 --- a/app/views/inbox/show.html.haml +++ b/app/views/inbox/show.html.haml @@ -1,7 +1,14 @@ -%ul +.container.j2-page - @inbox.each do |i| - %li - %strong - = user_screen_name i.question.user - asked - = i.question.content + .panel.inbox-box{'data-id' => i.id} + %p + %strong + = user_screen_name i.question.user + asked: + %p + %strong + = i.question.content + %textarea{name: 'ib-answer', 'data-id' => i.id} + %br/ + %button.btn.btn-success{name: 'ib-answer', 'data-ib-id' => i.id} + Answer