使用我当前的代码,发表评论的用户会收到通知,告诉他们他们发表了评论。
应该是发布评估的评估人员应该收到通知,通知他们有人对此进行了评论。
标准通知内容,但我哪里出错?!
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
如果您需要进一步 explanation or code 来帮助我,请告诉我们: - ]
链接是我使用的教程。
答案 0 :(得分:2)
我认为你正在寻找这个:
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) %>
答案 1 :(得分:1)
试试这个:
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) %>