If I write a comment on your status and then you comment back I don't get a notification to let me know you commented.
Currently only the owner of the status gets notifications. How can we make is where anyone who commented on a status should get notified too?
models
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
controllers
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
views
#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 %>
def create_notification
to make this work @brito – AnthonyGalli.com Sep 22 '15 at 18:37user: author,
, andauthor = valuation.user
(this is withincreate_notification
). So you need to create that specific notification passing theComment
's author instead of theValuation
author. – Leo Brito Sep 22 '15 at 18:54