Skip to content

A platform for community discussion. Free, open, simple.

FIX: Failed updates in tag group controller would be logged (#38333)

Gabriel Grubbae731ddef067f
diff --git a/app/controllers/tag_groups_controller.rb b/app/controllers/tag_groups_controller.rb
index 0da4fab14b6..5257f6ba512 100644
--- a/app/controllers/tag_groups_controller.rb
+++ b/app/controllers/tag_groups_controller.rb
@@ -67,9 +67,15 @@ class TagGroupsController < ApplicationController
guardian.ensure_can_admin_tag_groups!
old_data = TagGroupSerializer.new(@tag_group).to_json(root: false)
json_result(@tag_group, serializer: TagGroupSerializer) do |tag_group|
- @tag_group.update(tag_groups_params)
- new_data = TagGroupSerializer.new(@tag_group).to_json(root: false)
- StaffActionLogger.new(current_user).log_tag_group_change(@tag_group.name, old_data, new_data)
+ if @tag_group.update(tag_groups_params)
+ new_data = TagGroupSerializer.new(@tag_group).to_json(root: false)
+ StaffActionLogger.new(current_user).log_tag_group_change(
+ @tag_group.name,
+ old_data,
+ new_data,
+ )
+ true
+ end
end
end
diff --git a/frontend/discourse/app/components/tag-groups-form.gjs b/frontend/discourse/app/components/tag-groups-form.gjs
index deafff67468..4fba4b5a696 100644
--- a/frontend/discourse/app/components/tag-groups-form.gjs
+++ b/frontend/discourse/app/components/tag-groups-form.gjs
@@ -11,6 +11,7 @@ import BufferedProxy from "ember-buffered-proxy/proxy";
import DButton from "discourse/components/d-button";
import RadioButton from "discourse/components/radio-button";
import TextField from "discourse/components/text-field";
+import { popupAjaxError } from "discourse/lib/ajax-error";
import { AUTO_GROUPS } from "discourse/lib/constants";
import PermissionType from "discourse/models/permission-type";
import GroupChooser from "discourse/select-kit/components/group-chooser";
@@ -109,7 +110,10 @@ export default class TagGroupsForm extends Component {
return false;
}
- this.model.save(attrs).then(() => this.onSave?.());
+ this.model
+ .save(attrs)
+ .then(() => this.onSave?.())
+ .catch(popupAjaxError);
}
#serializeTag(t) {
diff --git a/spec/requests/tag_groups_controller_spec.rb b/spec/requests/tag_groups_controller_spec.rb
index 1533768c109..e58447c2ce5 100644
--- a/spec/requests/tag_groups_controller_spec.rb
+++ b/spec/requests/tag_groups_controller_spec.rb
@@ -356,5 +356,23 @@ RSpec.describe TagGroupsController do
expect(new_parent).to be_present
expect(tag_group.reload.parent_tag).to eq(new_parent)
end
+
+ it "returns an error when updating with a duplicate name" do
+ Fabricate(:tag_group, name: "existing_name")
+ original_name = tag_group.name
+
+ put "/tag_groups/#{tag_group.id}.json", params: { tag_group: { name: "existing_name" } }
+
+ expect(response.status).to eq(422)
+ expect(tag_group.reload.name).to eq(original_name)
+ end
+
+ it "does not create a staff action log entry when update fails" do
+ Fabricate(:tag_group, name: "existing_name")
+
+ expect {
+ put "/tag_groups/#{tag_group.id}.json", params: { tag_group: { name: "existing_name" } }
+ }.not_to change { UserHistory.where(action: UserHistory.actions[:tag_group_change]).count }
+ end
end
end