From 0e1bd5c75e0ed1d0f71adf94f63649b266a9c9b7 Mon Sep 17 00:00:00 2001 From: Julius Date: Mon, 25 Feb 2013 11:12:28 +0100 Subject: [PATCH] Improve (AJAX) deletion of StockArticles --- app/controllers/stockit_controller.rb | 6 +++--- app/views/stockit/_destroy_fail.js.haml | 8 +++----- app/views/stockit/destroy.js.haml | 15 ++++++++++++++- app/views/stockit/index.html.haml | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index f70bec27..2a232a31 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -32,9 +32,9 @@ class StockitController < ApplicationController end def destroy - StockArticle.find(params[:id]).destroy - render :layout => false, - :locals => { :destroyed_article_id => params[:id] } + @article = StockArticle.find(params[:id]) + @article.destroy + render :layout => false rescue => error render :partial => "destroy_fail", :layout => false, :locals => { :fail_msg => "Ein Fehler ist aufgetreten: " + error.message } diff --git a/app/views/stockit/_destroy_fail.js.haml b/app/views/stockit/_destroy_fail.js.haml index 60b03d87..a9601900 100644 --- a/app/views/stockit/_destroy_fail.js.haml +++ b/app/views/stockit/_destroy_fail.js.haml @@ -1,7 +1,5 @@ --# please polish the following line if you know how -var errorDiv = $('
×
'); - --# next line, createTextNode called by .text(textString) helps escaping for html, but what about ' signs? -errorDiv.text('#{fail_msg}'); +-# please polish the following line if you know how, same in view destroy +var errorDiv = $('
x
'); +errorDiv.append(document.createTextNode('#{j(fail_msg)}')); $('div.container-fluid').prepend(errorDiv); diff --git a/app/views/stockit/destroy.js.haml b/app/views/stockit/destroy.js.haml index 39602dfb..66d6d70d 100644 --- a/app/views/stockit/destroy.js.haml +++ b/app/views/stockit/destroy.js.haml @@ -1 +1,14 @@ -console.log('#{destroyed_article_id}'); +-# please polish the following line if you know how, same in partial _destroy_fail +var successDiv = $('
x
'); + +successDiv.append(document.createTextNode('Artikel #{j(@article.name)} gelöscht.')); +$('div.container-fluid').prepend(successDiv); + +-# WARNING: If you try to use the escape j(...) here, an error occurs: +-# Ein Fehler ist aufgetreten: undefined method `gsub' for 50:Fixnum +-# However, it should work without without escaping. +-# Note that article names which are purely numeric, e.g. 12345, are escaped correctly (see above). + +$('#stockArticle-#{@article.id}').remove(); + +-# WARNING: Do not use a simple .fadeOut() above, because it conflicts with the show/hide function of unavailable articles. diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index 6ceb0e91..c69c1689 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -44,7 +44,7 @@ %th %tbody - for article in @stock_articles - %tr{:class => stock_article_classes(article)} + %tr{:class => stock_article_classes(article), :id => "stockArticle-#{article.id}"} %td=h article.name %td= article.quantity %td= article.quantity - article.quantity_available