如何通过Noticed Gem发送Thredded论坛通知?
解决Thredded与Noticed通知冲突的方案
1. 彻底移除Thredded自带的通知图标
Thredded.notifiers = []仅能禁用通知的发送逻辑,无法移除页面上的通知图标。你需要:
- 先运行
rails generate thredded:views,把Thredded的视图文件复制到项目本地的app/views/thredded目录 - 打开
app/views/thredded/shared/_navigation.html.erb,删除或注释掉渲染通知图标的代码块,示例代码如下:
<% # 注释掉Thredded自带的通知导航项 %> <%# if thredded_current_user %> <%# <li class="thredded-nav-item"> %> <%# <%= link_to thredded.notifications_path, class: 'thredded-nav-link' do %> %> <%# <%= thredded_icon('notifications') %> %> <%# <span class="thredded-nav-link-text"><%= t('thredded.navigation.notifications') %></span> %> <%# <%= thredded_notifications_unread_count_badge %> %> <%# <% end %> %> <%# </li> %> <%# end %>
2. 让Thredded通过Noticed发送通知
你之前的代码错误在于初始化阶段直接调用deliver_later,此时不存在user变量导致崩溃。正确的做法是给Thredded注册自定义通知器:
- 在
config/initializers/thredded.rb中配置:
Thredded.notifiers = [ lambda do |event| # 根据Thredded事件类型匹配对应的Noticed通知类 case event when Thredded::NewPrivatePostEvent Noticed::PrivateMessageNotification.with(post: event.post).deliver_later(event.recipient) when Thredded::NewPostInTopicFollowingEvent Noticed::ForumPostNotification.with(post: event.post).deliver_later(event.recipient) # 其他Thredded事件(比如@提及通知)按需添加 end end ]
- 确保你已创建对应的Noticed通知类(如
PrivateMessageNotification),继承自Noticed::Base并配置好通知内容与渲染逻辑。
3. 统一通知入口
保留主导航栏的Noticed通知图标作为唯一入口,论坛页面不再显示Thredded的通知组件,所有通知统一通过Noticed的列表页面查看。
内容的提问来源于stack exchange,提问作者BethanyG
相关产品推荐
相关产品推荐

