简体   繁体   English

如何设置:user_id与评论通知?

[英]How to set :user_id with comment notifications?

Whenever a notification is created how can we set the :user_id for the user who made the comment (and in affect the notification)? 每当创建通知时,我们如何为发表评论的用户设置:user_id (并影响通知)?

When I check rails console for some reason all Notifications are showing up as user_id: 1 when some of them should be user_id: 2 since the latter was the one who did the commenting. 当我出于某种原因检查rails console ,所有Notifications都显示为user_id: 1 ,其中一些应为user_id: 2因为后者是进行评论的人。

I'm probably just not understanding the code well enough because I followed along this tutorial inputting my own version along the way. 我可能只是不太了解代码,因为我按照本教程的要求一直输入自己的版本。

comment.rb 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 :valuation
  belongs_to :user
  validates :user, presence: true

private

  def create_notification # I replaced the tutorial's "Post" with my "Valuation"
    @valuation = Valuation.find_by(self.valuation_id)
    @user = User.find_by(@valuation.user_id).id
    Notification.create(
     valuation_id: self.valuation_id,
     user_id: @user,
     comment_id: self,
     read: false
    )
  end
end

notifications_controller notifications_controller

class NotificationsController < ApplicationController
  before_action :correct_user, only: [:destroy]

    def index
    @notifications = Notification.where(:user_id => current_user.id)
    @notifications.each do |notification|
        notification.update_attribute(:read, true)
    end
    end


    def destroy
      @notification = Notification.find(params[:id])
      @notification.destroy
      redirect_to :back
    end

private

  def correct_user
    @notification = current_user.notifications.find_by(id: params[:id])
    redirect_to root_url, notice: "Not authorized to edit this notification" if @notification.nil?
  end
end

notifications/_notification 通知/通知

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

Please let me know if you need further explanation or code to help you help me :-] 如果您需要进一步的说明或代码来帮助您,请告诉我:-]

This 这个

@valuation = Valuation.find_by(self.valuation_id)
@user = User.find_by(@valuation.user_id).id

Notification.create(
     valuation_id: self.valuation_id,
     user_id: @user,
     comment_id: self,
     read: false
    )

should be 应该

self.notifications.create(
  valuation: self.valuation,
  user: self.valuation.user
)

in your migration file set: 在您的迁移文件集中:

t.boolean read, default: false

in your controller: 在您的控制器中:

def index
  @notifications = Notification.where(:user_id => current_user.id)
  @notifications.each do |notification|
    notification.update_attribute(:read, true)
  end
end

becomes: 变成:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM