From b65c60ea76aa0b8b0a7e748a461469a71170acf2 Mon Sep 17 00:00:00 2001 From: nilsding Date: Mon, 10 Nov 2014 18:19:04 +0100 Subject: [PATCH] we are now making AJAX requests --- app/assets/javascripts/application.coffee | 41 ------------- .../javascripts/application.js.erb.coffee | 59 +++++++++++++++++++ app/assets/stylesheets/application.css.scss | 1 + app/dir/x | 0 config/routes.rb | 2 +- 5 files changed, 61 insertions(+), 42 deletions(-) delete mode 100644 app/assets/javascripts/application.coffee create mode 100644 app/assets/javascripts/application.js.erb.coffee delete mode 100644 app/dir/x diff --git a/app/assets/javascripts/application.coffee b/app/assets/javascripts/application.coffee deleted file mode 100644 index 549d0699..00000000 --- a/app/assets/javascripts/application.coffee +++ /dev/null @@ -1,41 +0,0 @@ -#= require jquery -#= require jquery_ujs -#= require turbolinks -#= require bootstrap -#= require bootstrap-material-design -#= require nprogress -#= require nprogress-turbolinks -#= require_tree . - -NProgress.configure - showSpinner: false - -$(document).on "click", "button[name=qb-ask]", -> - btn = $(this) - btn.button "loading" - promote = btn[0].dataset.promote == "true" - $("textarea[name=qb-question]").attr "readonly", "readonly" - anonymousQuestion = if $("input[name=qb-anonymous]")[0] != undefined - $("input[name=qb-anonymous]")[0].checked - else - true - console.log "some AJAX magic should happen here!" - btn.button "reset" - $("textarea[name=qb-question]").removeAttr "readonly" - if true # data.success - $("textarea[name=qb-question]").val '' - if promote - $("div#question-box").hide() - $("div#question-box-promote").show() - else - $.snackbar # allahu snackbar - content: "Successfully asked question" - style: "snackbar" - timeout: 5000 - -$(document).on "click", "button#create-account", -> - Turbolinks.visit "/sign_up" - -$(document).on "click", "button#new-question", -> - $("div#question-box").show() - $("div#question-box-promote").hide() diff --git a/app/assets/javascripts/application.js.erb.coffee b/app/assets/javascripts/application.js.erb.coffee new file mode 100644 index 00000000..7f1e8024 --- /dev/null +++ b/app/assets/javascripts/application.js.erb.coffee @@ -0,0 +1,59 @@ +#= require jquery +#= require jquery_ujs +#= require turbolinks +#= require bootstrap +#= require bootstrap-material-design +#= require nprogress +#= require nprogress-turbolinks +#= require_tree . + +NProgress.configure + showSpinner: false + +$(document).on "click", "button[name=qb-ask]", -> + btn = $(this) + btn.button "loading" + promote = btn[0].dataset.promote == "true" + $("textarea[name=qb-question]").attr "readonly", "readonly" + anonymousQuestion = if $("input[name=qb-anonymous]")[0] != undefined + $("input[name=qb-anonymous]")[0].checked + else + true + $.ajax + url: '/ajax/ask' # TODO: find a way to use rake routes instead of hardcoding them here + data: + rcpt: $("input[name=qb-to]").val() + question: $("textarea[name=qb-question]").val() + anonymousQuestion: anonymousQuestion + success: (data, status, jqxhr) -> + if data.success + $("textarea[name=qb-question]").val '' + if promote + $("div#question-box").hide() + $("div#question-box-promote").show() + $.snackbar # allahu snackbar + content: "Successfully asked question" + 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#create-account", -> + Turbolinks.visit "/sign_up" + +$(document).on "click", "button#new-question", -> + $("div#question-box").show() + $("div#question-box-promote").hide() diff --git a/app/assets/stylesheets/application.css.scss b/app/assets/stylesheets/application.css.scss index f869c596..03fd4dab 100644 --- a/app/assets/stylesheets/application.css.scss +++ b/app/assets/stylesheets/application.css.scss @@ -10,5 +10,6 @@ body { padding-top: $navbar-height; } @import "font-awesome"; +$nprogress-color: #666; /* hail stan */ @import 'nprogress'; @import 'nprogress-bootstrap'; \ No newline at end of file diff --git a/app/dir/x b/app/dir/x deleted file mode 100644 index e69de29b..00000000 diff --git a/config/routes.rb b/config/routes.rb index 0ca1c69e..37f3155a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,7 +24,7 @@ Rails.application.routes.draw do match '/settings/profile', to: 'user#update', via: 'patch', as: :update_user_profile namespace :ajax do - match '/ask', to: 'ajax#ask', via: :post, as: :ajax_ask + match '/ask', to: 'ajax#ask', via: :post, as: :ask end match '/user/:username', to: 'user#show', via: 'get'