# Discourse API“更新分类”所需参数

**URL:** <https://meta.discourse.org/t/discourse-api-update-category-required-parameters/132424>\
**Category:** Development\
**Tags:** rest-api\
**Created:** [2019年十一月2日 00:47 UTC](https://meta.discourse.org/t/discourse-api-update-category-required-parameters/132424 "2019-11-02T00:47:20Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![jericson](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jericson/32/116215_2.png) [@jericson](https://meta.discourse.org/u/jericson)\
**Post date:** [2020年八月7日 15:56 UTC](https://meta.discourse.org/t/discourse-api-update-category-required-parameters/132424/2 "2020-08-07T15:56:14Z")

</div>

我也觉得这有点烦人。在更新类别的其他信息时，我未必知道该类别的 `color` 和 `text_color`。因此，我的选择要么是将两者都改为无害的值，要么是先添加另一个 API 调用来获取这些信息。以下是使用 [discourse\_api gem](https://github.com/discourse/discourse_api) 的示例：

```plaintext
response = client.categories

cat = response.find {|e| e['name'] == 'Category Name'}

response = client.update_category(
  id: cat['id'],
  name: cat['name'],
  color: cat['color'],
  text_color: cat['text_color'],

  # 在下面添加您实际想要更新的属性
  parent_category_id: 7, # 我将多个类别嵌套在 #7 下
)

```

显然，我也不总是知道类别名称。在更新类别之前，我确实需要先查询该名称。

如果这些参数不是必需的，那将会很方便。或者，是否可以使用某种语法来表示“我不想更改此字段”？例如，如果传入 `_`，则该属性保持为当前值。（我不确定为何这些字段是必需的，所以我提出这些想法，它们可能有也可能没有用。）

---

_[View the full topic](https://meta.discourse.org/t/discourse-api-update-category-required-parameters/132424)._
