# 将 digest\_custom\_html 作为 HTML 处理（之前：覆盖 digest.html.erb）

**URL:** <https://meta.discourse.org/t/fixing-digest-custom-html-to-be-treated-as-html-was-overriding-digest-html-erb/361988>\
**Category:** Development\
**Created:** [2025 年4 月 15 日 22:22 UTC](https://meta.discourse.org/t/fixing-digest-custom-html-to-be-treated-as-html-was-overriding-digest-html-erb/361988 "2025-04-15T22:22:18Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

作者： ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
发布日期： [2025 年4 月 28 日 20:43 UTC](https://meta.discourse.org/t/fixing-digest-custom-html-to-be-treated-as-html-was-overriding-digest-html-erb/361988/9 "2025-04-28T20:43:21Z")

</div>

> [@david](#):
>
> 你能给你的方法覆盖的结果加上`.html_safe`吗？

我的天哪。显然……我可以！！！！我从来没想过我能把它放在那里，而不是放在模板的顶部（至少在我脑子里是这样渲染的）。

我现在看到并理解了 `digest_custom` 以及它在代码中调用 `.html_safe`，它很漂亮，就在我查看并试图理解的代码中，但它使用那种奇怪的无括号语法让我感到困惑（对我来说）。（或者我可能永远也看不到它）。

> [@david](#):
>
> 但这不是这些方法的“预期”用途。如果这是一个故意的插件 api，它应该在 plugin/instance.rb 中与其他方法一起。

嗯，我确实在 locales 中看到了这些值，但与我之前覆盖模板相比，这是一个风险小得多的解决方案。

非常感谢你指明了正确的方向！

这是我还在做的另一件我觉得很傻的事情。我的 `digest_custom_html` 需要它被渲染的用户，所以我正在覆盖 `digest` 来设置 `@user`，这样我的 `digets_custom_html` 就可以访问 digest 所属的用户。有没有什么超级简单的方法可以避免这样做？

```plaintext
after_initialize do
  # Code which should run after Rails has finished booting
  # 在 Rails 完成启动后应运行的代码

  # require_relative \"lib/discourse_add_jobs_to_digest/user_notifications_helper_override\"
  require_relative \"lib/discourse_add_jobs_to_digest/engine\"
  require_relative \"lib/discourse_add_jobs_to_digest/job_api\"

  require_dependency \"user_notifications\"
  module ::UserNotificationsHelperOverride
    def digest_custom_html(position_key)
      if position_key == \"above_footer\"
        DiscourseAddJobsToDigest::JobApi.get_jobs_html(@user).html_safe
      else
        super
      end
    end
  end

  UserNotificationsHelper.prepend(::UserNotificationsHelperOverride)

  module ::UserNotificationsOverride
    def digest(user, opts = {})
      @user = user
      super
    end
  end
  UserNotifications.prepend(::UserNotificationsOverride)
end

```

---

_[View the full topic](https://meta.discourse.org/t/fixing-digest-custom-html-to-be-treated-as-html-was-overriding-digest-html-erb/361988)._
