diff --git a/app/controllers/avo/base_controller.rb b/app/controllers/avo/base_controller.rb index 8f388c06b6..a47bb034d7 100644 --- a/app/controllers/avo/base_controller.rb +++ b/app/controllers/avo/base_controller.rb @@ -504,8 +504,10 @@ def destroy_success_action end def destroy_fail_action + flash[:error] = destroy_fail_message + respond_to do |format| - format.html { redirect_back fallback_location: params[:referrer] || resources_path(resource: @resource, turbo_frame: params[:turbo_frame], view_type: params[:view_type]), error: destroy_fail_message } + format.turbo_stream { render partial: "avo/partials/flash_alerts" } end end diff --git a/app/views/avo/partials/_flash_alerts.turbo_stream.erb b/app/views/avo/partials/_flash_alerts.turbo_stream.erb index 9733d23012..a3b1c2a35f 100644 --- a/app/views/avo/partials/_flash_alerts.turbo_stream.erb +++ b/app/views/avo/partials/_flash_alerts.turbo_stream.erb @@ -1,3 +1,3 @@ <%= turbo_stream.append "alerts" do %> - <%= render Avo::FlashAlertsComponent.new flashes: flash %> + <%= render Avo::FlashAlertsComponent.new flashes: flash.discard %> <% end %> diff --git a/spec/system/avo/has_many_spec.rb b/spec/system/avo/has_many_spec.rb index c58a702e8d..214108dbe7 100644 --- a/spec/system/avo/has_many_spec.rb +++ b/spec/system/avo/has_many_spec.rb @@ -64,6 +64,42 @@ sleep 0.8 expect(page).to have_text("Record destroyed").twice end + + it "shows the notification when delete fails" do + Comment.class_eval do + def destroy + raise "Record failed to destroy" + end + end + + visit url + + expect { + find("[data-resource-id='#{comments.first.id}'] [data-control='destroy']").click + sleep 0.2 + page.driver.browser.switch_to.alert.accept + sleep 0.2 + find("[data-resource-id='#{comments.third.id}'] [data-control='destroy']").click + sleep 0.2 + page.driver.browser.switch_to.alert.accept + sleep 0.2 + }.to change(Comment, :count).by(0) + + expect(page).to have_current_path url + + expect(page).to have_text comments.third.tiny_name.to_s + expect(page).to have_text comments.first.tiny_name.to_s + expect(page).to have_text comments.second.tiny_name.to_s + + sleep 0.8 + expect(page).to have_text("Record failed to destroy").twice + + Comment.class_eval do + def destroy + super + end + end + end end end