如何将通知发送给其post收到评论的用户?

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

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

标准通知的东西,但我哪里出错?!

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 

通知/指数

  commented on  

valuation.rb

 class Valuation { 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 

如果您需要进一步的解释或代码来帮助我,请告诉我: – ]

链接是我使用的教程。

试试这个:

  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) %> 

我想你正在寻找这个:

 def create_notification @valuation = Valuation.find_by(self.valuation_id) @comment = Comment.find_by(self.id) @user = User.find_by(@valuation.user_id).id self.notifications.create( comment: self, valuation: self.valuation, user:@valuation.user, read: false ) end 

对于通知索引,请尝试:

 <%= link_to Comment.find_by(notification.comment_id).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) %>