Se scrivo un commento sul vostro stato e poi commentate, non ricevo una notifica per farmi sapere che avete commentato.Come avvisare il commentatore?
Attualmente solo il proprietario dello stato riceve le notifiche. Come possiamo far sapere se anche chi ha commentato uno stato dovrebbe ricevere una notifica?
modelli
class Comment < ActiveRecord::Base
after_save :create_notification
has_many :notifications
def create_notification
author = valuation.user
notifications.create(
comment: self,
valuation: valuation,
user: author,
read: false
)
end
end
class Notification < ActiveRecord::Base
belongs_to :comment
belongs_to :valuation
belongs_to :user
end
class Valuation < ActiveRecord::Base
belongs_to :user
has_many :notifications
has_many :comments
end
controllori
class CommentsController < ApplicationController
before_action :set_commentable, only: [:index, :new, :create]
before_action :set_comment, only: [:edit, :update, :destroy]
def create
@comment = @commentable.comments.new(comment_params)
if @comment.save
redirect_to @commentable, notice: "Comment created."
else
render :new
end
end
private
def set_commentable
@commentable = find_commentable
end
def set_comment
@comment = current_user.comments.find(params[:id])
end
def find_commentable
if params[:goal_id]
Goal.find(params[:goal_id])
elsif params[:habit_id]
Habit.find(params[:habit_id])
elsif params[:valuation_id]
Valuation.find(params[:valuation_id])
elsif params[:stat_id]
Stat.find(params[:stat_id])
end
end
end
class NotificationsController < ApplicationController
def index
@notifications = current_user.notifications
@notifications.each do |notification|
notification.update_attribute(:read, true)
end
end
end
vista
#notifications/_notification.html.erb
commented on <%= link_to "your value", notification_valuation_path(notification, notification.valuation_id) %>
#comments/_form.html.erb
<%= form_for [@commentable, @comment] do |f| %>
<%= f.text_area :content %>
<% end %>
La tua domanda è troppo generica. _ "ogni volta che un utente commenta su uno stato qualsiasi commento successivo a quello stato ** dovrebbe avvisarti ** che è stato fatto un commento aggiuntivo" _. Che cosa ti serve sapere? Stai chiedendo come sapere _quando_informare, o stai chiedendo di _how_ per avvisare l'utente? Cosa intendi esattamente per _notifying_? Qualche valore modificato in un modello, un'email inviata, un popup ... – brito
Se commento il tuo valore. Quindi se commentate di nuovo quel valore. Non ricevo una notifica per informarmi che hai commentato. Solo la persona di cui ottiene il valore è la notifica. Forse c'è un modo nel 'def create_notification' per rendere questo lavoro @brito –
@ AnthonyGalli.com Sì, perché la notifica viene sempre creata con' user: author, ', e' author = valuation.user' (questo è all'interno di ' create_notification'). Quindi è necessario creare quella specifica notifica passando l'autore del 'Comment' invece dell'autore' Valutazione '. – brito