# 在用户账户页面显示用户自定义字段

**URL:** https://meta.discourse.org/t/put-user-custom-field-on-the-user-account-page/134090
**Category:** Development
**Created:** [2019 年11 月 22 日 00:02 UTC](https://meta.discourse.org/t/put-user-custom-field-on-the-user-account-page/134090 "2019-11-22T00:02:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2019 年11 月 22 日 00:02 UTC](https://meta.discourse.org/t/put-user-custom-field-on-the-user-account-page/134090/1 "2019-11-22T00:02:51Z")

</div>

有人希望在账户页面添加用户自定义字段（出于某些原因，他们不喜欢个人资料页面）。

看起来我应该在主题组件中实现这一点。

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/templates/preferences/account.hbs#L207](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/templates/preferences/account.hbs#L207)

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/templates/preferences/profile.hbs#L24-L28](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/templates/preferences/profile.hbs#L24-L28)

`common/head_tag.html`：

```plaintext
<script type="text/x-handlebars" data-template-name="/connectors/user-preferences-account/lc-ucf">
<div style="height: 25px; width: 25px;background: blue"></div>
<h3>{{ model.username}} 的自定义用户字段</h3>
这是 uf：{{model.user_fields}}。
{{log "THIS" model.user_fields}}
{{#each model.user_fields |uf|}}
{{log "THISTHISTHIS" this}}
  <div class="control-group">
    {{user-field field=uf.field value=value}}
  </div>
{{/each}}
<div class='clearfix'></div>
</script>

```

但我似乎无法访问用户自定义字段。

我能看到我的蓝色方框。

我可以看到可以访问 `model.username`。

在控制台日志中，我看到：

```
THIS {3: "STEM 专业人士（行业）"}

```

 ![image](https://global.discourse-cdn.com/meta/original/3X/2/9/29b5eac341fc18afb194049c77d5e76b4c855a33.png)

我肯定是在做一些愚蠢的事情，但在反复阅读了出色的 [Discourse 主题开发者指南](https://meta.discourse.org/t/developer-s-guide-to-discourse-themes/93648#heading--4-c-1) 之后，我仍然一头雾水。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2019 年11 月 23 日 00:29 UTC](https://meta.discourse.org/t/put-user-custom-field-on-the-user-account-page/134090/2 "2019-11-23T00:29:20Z")

</div>

好吧，我又在发关于我正在尝试解决的某个问题的帖子了……

这次我试图在插件中解决同样的问题。我创建了 `myplugin/assets/javascripts/discourse/connectors/preferences/account/user-custom-controls/my-stuff.hbs`，它确实向页面添加了一些内容（和之前一样！），但我仍然不知道如何：(1) 获取数据，(2) 告诉 Ember（？）在表单保存时更新该字段。

我现在觉得，也许我需要覆盖或修改 `account.js.es6`，并将 `user_fields` 添加到 `this.saveAttrNames` 中，这样它就知道在更新时保存它们，同时也将数据加载到 `userFields` 中。

我已经查看了不少插件，但始终没能完全搞定。

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [2019 年11 月 23 日 03:26 UTC](https://meta.discourse.org/t/put-user-custom-field-on-the-user-account-page/134090/3 "2019-11-23T03:26:48Z")

</div>

好的，我已经让它运行了，但有点棘手。

创建一个包含以下代码的连接类。

```plaintext
import { set } from "@ember/object";
import EmberObject from "@ember/object";

export default {
@discourseComputed("model.user_fields.@each.value")
  publicUserFields() {
    const siteUserFields = this.site.get("user_fields");
    if (!isEmpty(siteUserFields)) {
      const userFields = this.get("model.user_fields");
      return siteUserFields
        .filterBy("show_on_user_card", true)
        .sortBy("position")
        .map(field => {
          set(field, "dasherized_name", field.get("name").dasherize());
          const value = userFields ? userFields[field.get("id")] : null;
          return isEmpty(value) ? null : EmberObject.create({ value, field });
        })
        .compact();
    }
  },
}

```

在连接模板中：

```plaintext
{{#if publicUserFields}}
        <div class="card-row fifth-row">
          <div class="public-user-fields">
            {{#each publicUserFields as |uf|}}
              {{#if uf.value}}
                <div class="public-user-field {{uf.field.dasherized_name}}">
                  <span class="user-field-name">{{uf.field.name}}:</span>
                  <span class="user-field-value">{{uf.value}}</span>
                </div>
              {{/if}}
            {{/each}}
          </div>
        </div>
{{/if}}

```

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [2019 年11 月 23 日 11:48 UTC](https://meta.discourse.org/t/put-user-custom-field-on-the-user-account-page/134090/4 "2019-11-23T11:48:13Z")

</div>

@pfaffman 成功了么？

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2019 年11 月 23 日 15:16 UTC](https://meta.discourse.org/t/put-user-custom-field-on-the-user-account-page/134090/5 "2019-11-23T15:16:19Z")

</div>

我还没有机会尝试，但我猜它会。我还有太多要学习的东西。非常感谢您的慷慨！我无以言谢，但等我试用后会再向您表达谢意。
