如何向帖子收到评论的用户发送通知? [英] How to send Notifications to the User whose post received a Comment?

查看:75
本文介绍了如何向帖子收到评论的用户发送通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我当前的代码,发表评论的用户会收到一条通知,告诉他们他们发表了评论.

With my current code the user who made the comment gets a notification telling them that they made a comment.

应该是发布估值的人受到评论,谁应该收到通知,通知他们有人评论了它.

It should be the person who posted the Valuation that got commented on who should get a notification notifying them that someone commented on it.

标准的通知内容,但我哪里出错了?!

Standard notification stuff, but where did I go wrong?!

comment.rb

class Comment < ActiveRecord::Base
  after_create :create_notification
  has_many :notifications
  has_many :comment_likes   
  has_many :likers, through: :comment_likes, class_name: 'User', source: :liker
  belongs_to :habit
  belongs_to :quantified
  belongs_to :valuation
  belongs_to :goal
  belongs_to :user
  validates :user, presence: true

private

  def create_notification # This is definitely the issue I made a lot of attempts to fix it, but now it's even more confusing
    @valuation = Valuation.find_by(self.valuation_id)
    @comment = Comment.find_by(self.id)
    @user = User.find_by(@comment.user_id).id
      self.notifications.create(
        comment: self,
        valuation: self.valuation,
        user: self.user,
        read: false
      )
  end
end

notification.rb

class Notification < ActiveRecord::Base
  belongs_to :comment
  belongs_to :valuation
  belongs_to :user
end

notifications_controller.rb

  def index
    @notifications = current_user.notifications
    @notifications.each do |notification|
      notification.update_attribute(:read, true) 
    end
  end

通知/索引

<%= link_to notification.user_id, user_path(Comment.find_by(notification.comment_id).user.id) %> commented on <%= link_to "your value", notification_valuation_path(notification, notification.valuation_id) %>

valuation.rb

class Valuation < ActiveRecord::Base
    belongs_to :user
    has_many :activities
  acts_as_taggable
  has_many :combine_tags
  has_many :notifications
  validates :name, presence: true
    has_many :notes, as: :notable
    scope :publish, ->{ where(:conceal => false) }
    has_many :notes
  has_many :valuation_likes 
  has_many :likers, through: :valuation_likes, class_name: 'User', source: :liker
  has_many :comment_likes
    has_many :comments

  scope :randomize, -> do
      order('RANDOM()').
      take(1)
    end

    scope :top_25, -> do
      order('RANDOM()').
      limit(25)
    end
end

如果您需要更多信息,请告诉我解释或代码来帮助你帮助我:-]

Please let me know if you need further explanation or code to help you help me :-]

链接是我用过的教程.

推荐答案

试试这个:

   def create_notification
      self.notifications.create(
        comment: self,
        valuation:  self.valuation,
        user: self.valuation.user, 
        read: false
      )
   end

通知链接:

   <%= link_to notification.comment.user_id, user_path(notification.comment.user_id) %> commented on <%= link_to "your value", notification_valuation_path(notification, notification.valuation_id) %>      

这篇关于如何向帖子收到评论的用户发送通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆