# 尝试取消星标'Bug Reporter'徽章时出现'FAILED'错误

**URL:** <https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523>\
**Category:** Bug\
**Tags:** badges, pr-welcome\
**Created:** [2024年十月12日 02:41 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523 "2024-10-12T02:41:44Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)\
**Post date:** [2024年十月12日 02:41 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/1 "2024-10-12T02:41:44Z")

</div>

尝试移除我的“Bug Reporter”徽章上的星标以使用其他徽章作为我最喜欢的徽章时，我收到了一个“失败”的错误。其他徽章可以取消星标（这是个词吗？），但这个徽章无法取消星标。

---

<div class="post-metadata">

**Author:** ![HamMan2118](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/hamman2118/32/383565_2.png) [@HamMan2118](https://meta.discourse.org/u/HamMan2118)\
**Post date:** [2024年十月12日 10:21 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/2 "2024-10-12T10:21:18Z")

</div>

我可以很好地取消星标。\n

\n会不会是视频中你的网络连接中断导致的问题？只是瞎猜

---

<div class="post-metadata">

**Author:** ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)\
**Post date:** [2024年十月13日 01:28 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/3 "2024-10-13T01:28:57Z")

</div>

我在稳定的 Wi-Fi 下试过了。仍然收到错误。

---

<div class="post-metadata">

**Author:** ![pangbo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pangbo/32/538562_2.png) [@pangbo](https://meta.discourse.org/u/pangbo)\
**Post date:** [2024年十月13日 07:45 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/4 "2024-10-13T07:45:56Z")

</div>

这个 bug 已经存在很长时间了。我去年研究过它，以下是重现它的步骤：

1. 作为管理员，将“最多收藏徽章”设置为 2，并创建至少一个可以多次获得的徽章。
2. 作为普通用户，将两个徽章设置为收藏，其中一个是可多次获得的徽章（称为徽章 A）。
3. 作为管理员，再次授予用户徽章 A。
4. 作为普通用户，刷新页面并尝试取消收藏徽章 A。您将遇到错误。

错误的原因是，每次授予可多次获得的徽章时，都会在数据库中创建一个新的 `user_badge` 记录。但是，当用户收藏了该徽章并再次获得它时，新的 `user_badge` 记录不会自动标记为 `is_favorite`。当用户尝试取消收藏徽章 A 时，前端默认会发送最新的 `user_badge` ID。由于此记录未标记为 `is_favorite`，后端会认为用户正在尝试将新徽章设为收藏（而不是取消收藏），这超出了 `max favorite badges` 的限制，从而导致错误。

相关代码位于：

> <https://github.com/discourse/discourse/blob/d3f09f8f61ac33f107fe1503fb4fd928877cfcd4/app/controllers/user_badges_controller.rb#L124-L141>

一个可能的解决方案是将第 131 行修改为：

```ruby
if UserBadge.where(badge: user_badge.badge, user: user_badge.user).pluck(:is_favorite).any? &&

```

然而，这并没有完全解决数据库记录中的不一致问题。

作为临时解决方法，普通用户可以通过在控制台中运行以下 JavaScript 代码来取消收藏所有徽章：

```javascript
const user_name = require("discourse/models/user").default.current().username;
const badges = await require("discourse/models/user-badge").default.findByUsername(user_name);
const favorites = new Map();
badges.filter((b)=>b.is_favorite).forEach((b)=>favorites.set(b.badge_id,b));
favorites.forEach((b)=>b.favorite());

```

---

<div class="post-metadata">

**Author:** ![Turtle](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/turtle/32/476220_2.png) [@Turtle](https://meta.discourse.org/u/Turtle)\
**Post date:** [2025年三月19日 21:57 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/5 "2025-03-19T21:57:24Z")

</div>

> [@pangbo](#):
>
> ```plaintext
> const user_name = require("discourse/models/user").default.current().username;
> const badges = await require("discourse/models/user-badge").default.findByUsername(user_name);
> const favorites = new Map();
> badges.filter((b)=>b.is_favorite).forEach((b)=>favorites.set(b.badge_id,b));
> favorites.forEach((b)=>b.favorite());
> 
> ```

我刚刚遇到了这个错误，但是将代码复制并粘贴到控制台中会返回一个语法错误：

> SyntaxError: 意外的标识符 ‘require’。变量声明后应为 ‘;’。  
> SyntaxError: 意外的标识符 ‘identifier’

也许这有点过时了？有人有更新的版本吗？

---

<div class="post-metadata">

**Author:** ![twofoursixeight](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/twofoursixeight/32/574822_2.png) [@twofoursixeight](https://meta.discourse.org/u/twofoursixeight)\
**Post date:** [2025年三月19日 22:06 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/6 "2025-03-19T22:06:23Z")

</div>

我还想补充：

 ![“Turtle Leader”的社交媒体帖子称之前的解决方案不起作用，但他们通过使用不同的徽章和重新排列一些元素找到了一个解决方法，并感谢之前的帮助。（由 AI 添加字幕）](https://global.discourse-cdn.com/meta/original/4X/4/4/1/441c913ce44e3e8ed3dbc571a0137738c4ea0ec9.jpeg)

---

<div class="post-metadata">

**Author:** ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)\
**Post date:** [2025年三月20日 00:21 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/7 "2025-03-20T00:21:08Z")

</div>

好吧，我取消了我对另一个徽章（善解人意）的星标，然后我就可以取消对 Bug Reporter 徽章的星标了。

之后，如果我给善解人意徽章加上星标，这是有效的，但给 Aficionado 徽章加上星标却返回了那个错误。非常奇怪。

编辑：在每次重新添加星标之间重新加载页面后，我现在已经删除了（讽刺的是）有 bug 的 Bug Reporter 徽章。

---

<div class="post-metadata">

**Author:** ![pangbo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pangbo/32/538562_2.png) [@pangbo](https://meta.discourse.org/u/pangbo)\
**Post date:** [2025年四月18日 10:37 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/8 "2025-04-18T10:37:13Z")

</div>

我创建了一个拉取请求来修复此问题：

> <https://github.com/discourse/discourse/pull/32369>
>
> This PR addresses the issue discussed in https://meta.discourse.org/t/failed-err…or-when-trying-to-un-star-the-bug-reporter-badge/330523
> 
> 1. Fixed an issue in \`user\_badges\_controller.rb\` where the \`toggle\_favorite\` method could incorrectly treat badges already marked as favorite as not being favorite.  
> 2. Automatically synchronize the \`is\_favorite\` status for new badges when a user is granted a multiple-grant badge.

---

<div class="post-metadata">

**Author:** ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)\
**Post date:** [2025年四月21日 05:30 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/9 "2025-04-21T05:30:06Z")

</div>

嘿，感谢您的贡献！我已审阅您的 PR，需要做一些修改。

---

<div class="post-metadata">

**Author:** ![pangbo](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pangbo/32/538562_2.png) [@pangbo](https://meta.discourse.org/u/pangbo)\
**Post date:** [2025年四月21日 09:42 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/10 "2025-04-21T09:42:22Z")

</div>

感谢您的审阅，我已更新此拉取请求。

---

<div class="post-metadata">

**Author:** ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)\
**Post date:** [2025年四月22日 07:59 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/12 "2025-04-22T07:59:47Z")

</div>

太棒了！这已获批准并已合并。

更改应很快在 meta 上生效。

---

<div class="post-metadata">

**Author:** ![nat](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nat/32/235063_2.png) [@nat](https://meta.discourse.org/u/nat)\
**Post date:** [2025年四月26日 00:00 UTC](https://meta.discourse.org/t/failed-error-when-trying-to-un-star-the-bug-reporter-badge/330523/13 "2025-04-26T00:00:56Z")

</div>

此主题已在 3 天后自动关闭。不再允许回复。
