如何从注意到的宝石中创建动态参数

发布于 2025-02-04 08:32:27 字数 1274 浏览 2 评论 0原文

我正在使用注意到GEM进行通知,并试图以更清洁的方式使用户获得与帖子和对话相关的通知。该通知将帖子存储为参数中的对象。我遇到了很多重复的代码,因为我的小组中有对话中的对话,并与其中的帖子进行了讨论。因此,我有一个“ post_notification”和一个“对话_notification”。因此,当用户检查他的通知时,这是现在对我有用的。

  - if notification.params[:conversation]
    - if avatar_url_for(notification.to_notification.params[:conversation].user).present?
      = image_tag avatar_url_for(notification.to_notification.params[:conversation].user, size: 35), class: "rounded-circle"
    - else
      = image_tag "no-photo-avatar.png", class: "rounded-circle"
  - if notification.params[:post]
    - if avatar_url_for(notification.to_notification.params[:post].user).present?
      = image_tag avatar_url_for(notification.to_notification.params[:post].user, size: 35), class: "rounded-circle"
    - else
      = image_tag "no-photo-avatar.png", class: "rounded-circle"
  - noticed_notification = notification.to_notification
  = link_to noticed_notification.message, noticed_notification.url
   by:
  - if notification.params[:conversation]
    = notification.params[:conversation].user.full_name
  - if notification.params[:post]
    = notification.params[:post].user.full_name

如何使它更好地不重复或动态?我想创建一种方法,以使其更清洁而不重复,以便我可以在Navbar Nevbar Notification下拉和通知页面上使用它

I am using Noticed gem for my notifications and I am trying to get the users for the notifications related to the post and conversation in a cleaner way. The notification stores the post as an object inside params. I'm running into a lot of repeating code because I have Groups with conversations in it and Discussions with posts in it. So I have a "post_notification" and a "conversation_notification". So when a user checks his notifications this is what's working for me right now.

  - if notification.params[:conversation]
    - if avatar_url_for(notification.to_notification.params[:conversation].user).present?
      = image_tag avatar_url_for(notification.to_notification.params[:conversation].user, size: 35), class: "rounded-circle"
    - else
      = image_tag "no-photo-avatar.png", class: "rounded-circle"
  - if notification.params[:post]
    - if avatar_url_for(notification.to_notification.params[:post].user).present?
      = image_tag avatar_url_for(notification.to_notification.params[:post].user, size: 35), class: "rounded-circle"
    - else
      = image_tag "no-photo-avatar.png", class: "rounded-circle"
  - noticed_notification = notification.to_notification
  = link_to noticed_notification.message, noticed_notification.url
   by:
  - if notification.params[:conversation]
    = notification.params[:conversation].user.full_name
  - if notification.params[:post]
    = notification.params[:post].user.full_name

How can I make this better not repeating or dynamic? I wanted to create a method where it's cleaner and not repeating so I can use it on the navbar notification dropdown and notification page

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文