# 自定义插件：无法添加到序列化器

**URL:** https://meta.discourse.org/t/custom-plugin-cannot-add-to-serializer/150607
**Category:** Development
**Created:** [2020年五月6日 14:53 UTC](https://meta.discourse.org/t/custom-plugin-cannot-add-to-serializer/150607 "2020-05-06T14:53:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![marek](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/marek/32/178390_2.png) [@marek](https://meta.discourse.org/u/marek)
#### Post date: [2020年五月6日 14:53 UTC](https://meta.discourse.org/t/custom-plugin-cannot-add-to-serializer/150607/1 "2020-05-06T14:53:38Z")

</div>

你好。  
我尝试将“用户是否为管理员”（true/false）字段添加到 BasicUserSerializer 中，以便在“关于”页面等地方使用。

```
#customization:plugin.rb
after_initialize do
  add_to_serializer(:basic_user, :admin) do
    object.admin
  end
end

```

但每次我像这样在插件中添加该字段时，除了“关于”页面外，其他所有页面都无法加载（尽管我确实能看到该参数已成功添加），并在 ListController#latest 中收到 ActiveModel::MissingAttributeError 错误：缺少属性 admin。

如何避免这种情况？

---

<div class="post-metadata">

### Author: ![Mgcs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mgcs/32/202507_2.png) [@Mgcs](https://meta.discourse.org/u/Mgcs)
#### Post date: [2021年三月17日 01:50 UTC](https://meta.discourse.org/t/custom-plugin-cannot-add-to-serializer/150607/2 "2021-03-17T01:50:21Z")

</div>

你做到这一步了吗？
