From d050c04bb0dca066d59b9ffc8f82e83113743909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 6 Mar 2024 05:05:29 +0100 Subject: [PATCH] Use a button to destroy poll question answer images As mentioned in commits 5311daadf and bb958daf0, using links combined with JavaScript to generate POST (or, in this case, DELETE) requests to the server has a few issues. Note that the AJAX response stopped working after replacing the link with a button. Not sure about the reason, but, since this is one of the very few places where we use AJAX calls to delete content, the easiest solution is to stop using AJAX and be consistent with what we do in the rest of the admin section. --- .../poll/questions/answers/images_controller.rb | 5 ++--- .../poll/questions/answers/images/destroy.js.erb | 3 --- .../poll/questions/answers/images/index.html.erb | 14 ++++++++------ config/locales/en/responders.yml | 1 + config/locales/es/responders.yml | 1 + .../poll/questions/answers/images/images_spec.rb | 4 ++-- 6 files changed, 14 insertions(+), 14 deletions(-) delete mode 100644 app/views/admin/poll/questions/answers/images/destroy.js.erb diff --git a/app/controllers/admin/poll/questions/answers/images_controller.rb b/app/controllers/admin/poll/questions/answers/images_controller.rb index 48b843d72..768f8a532 100644 --- a/app/controllers/admin/poll/questions/answers/images_controller.rb +++ b/app/controllers/admin/poll/questions/answers/images_controller.rb @@ -25,9 +25,8 @@ class Admin::Poll::Questions::Answers::ImagesController < Admin::Poll::BaseContr def destroy @image.destroy! - respond_to do |format| - format.js { render layout: false } - end + redirect_to admin_answer_images_path(@image.imageable), + notice: t("flash.actions.destroy.poll_question_answer_image") end private diff --git a/app/views/admin/poll/questions/answers/images/destroy.js.erb b/app/views/admin/poll/questions/answers/images/destroy.js.erb deleted file mode 100644 index 137451dda..000000000 --- a/app/views/admin/poll/questions/answers/images/destroy.js.erb +++ /dev/null @@ -1,3 +0,0 @@ -$(".delete").on("ajax:success", function () { - $(this).closest("div").fadeOut(); -}); diff --git a/app/views/admin/poll/questions/answers/images/index.html.erb b/app/views/admin/poll/questions/answers/images/index.html.erb index e3f2b3e92..957c56d76 100644 --- a/app/views/admin/poll/questions/answers/images/index.html.erb +++ b/app/views/admin/poll/questions/answers/images/index.html.erb @@ -26,12 +26,14 @@ <%= render_image(image, :large, true) if image.present? %> <% if can?(:destroy, image) %> - <%= link_to t("images.remove_image"), - admin_image_path(image), - class: "delete float-right", - method: :delete, - remote: true, - data: { confirm: t("admin.actions.confirm_action", action: t("images.remove_image"), name: image.title) } %> + <%= render Admin::ActionComponent.new( + :destroy, + image, + text: t("images.remove_image"), + method: :delete, + confirm: t("admin.actions.confirm_action", action: t("images.remove_image"), name: image.title), + class: "delete float-right" + ) %> <% end %> <% end %> diff --git a/config/locales/en/responders.yml b/config/locales/en/responders.yml index 31e9350b0..b4d2799ce 100644 --- a/config/locales/en/responders.yml +++ b/config/locales/en/responders.yml @@ -34,6 +34,7 @@ en: destroy: budget_investment: "Investment project deleted successfully." topic: "Topic deleted successfully." + poll_question_answer_image: "Image deleted successfully." poll_question_answer_video: "Answer video deleted successfully." valuator_group: "Valuator group deleted successfully" vote: "Vote deleted successfully" diff --git a/config/locales/es/responders.yml b/config/locales/es/responders.yml index f35f42a96..8928540e5 100644 --- a/config/locales/es/responders.yml +++ b/config/locales/es/responders.yml @@ -34,6 +34,7 @@ es: destroy: budget_investment: "Proyecto de gasto eliminado." topic: "Tema eliminado." + poll_question_answer_image: "Imagen eliminada correctamente." poll_question_answer_video: "VĂ­deo de respuesta eliminado." valuator_group: "Grupo de evaluadores eliminado correctamente" vote: "Voto eliminado correctamente" diff --git a/spec/system/admin/poll/questions/answers/images/images_spec.rb b/spec/system/admin/poll/questions/answers/images/images_spec.rb index cfbc12293..a6e35acc8 100644 --- a/spec/system/admin/poll/questions/answers/images/images_spec.rb +++ b/spec/system/admin/poll/questions/answers/images/images_spec.rb @@ -73,7 +73,7 @@ describe "Images", :admin do expect(page).to have_content image.title accept_confirm "Are you sure? Remove image \"#{image.title}\"" do - click_link "Remove image" + click_button "Remove image" end expect(page).not_to have_css "img[title='#{image.title}']" @@ -88,7 +88,7 @@ describe "Images", :admin do expect(page).to have_css "img[title='#{image.title}']" expect(page).to have_content image.title - expect(page).not_to have_link "Remove image" + expect(page).not_to have_content "Remove image" expect(page).to have_content "Once the poll has started it will not be possible to create, edit or" end end