If its not too much to ask. Can you please make a similar video for **activity_notification** gem? Its almost identical to this one, but it may be more optimized. https://github.com/simukappu/activity_notification
I've had a chance to look at this gem. Its documentation is pretty extensive which is always nice to see. I'll consider covering this gem in the future. One thing that I highly dislike about this gem is that it seems a lot more invasive in the models. It also has a lot of added complexity based on the documentation and I'm not convinced that it's necessary to accomplish what they're doing. Overall the gem does look pretty cool and for a large complex app, this may be a good fit.
Hey ☒ ... I'm having some trouble implementing the notifications feature into my rails app.
The notifications counter updates perfectly in my app layout view, but when I try to click on the notifications url I keep receiving a 500 NameError and it's parsing through and fussing with a link to root_path in my app/layout which doesn't make sense, to me as I know that works fine.
in my comment.rb model
class Comment < ApplicationRecord
after_commit :create_notifications, on: :create
belongs_to :commentable, polymorphic: true
has_many :comments, as: :commentable
belongs_to :user, optional: true
validates :body, presence: true
def deleted?
user.nil?
end
private
def create_notifications
Notification.create do |notification|
notification.notify_type = 'commentable'
notification.actor = self.user
notification.user = self.commentable.user
notification.target = self
notification.second_target = self.commentable
end
end
end
in my views/notifications/_commentable.html.erb
<div class=''>
<%= link_to notification.actor.email, main_app.user_path(notification.actor) %> has commented in
</div>
<div class=''>
<%= notification.target.body %>
</div>
Do you have any ideas as to what might be creating the fuss?
It looks like the engine is trying to render the partial because of the "duplicate" notifications in the path timesink/app/views/notifications/notifications/_notification.html.erb
I've been having trouble continuing to set this up with my nested Comments which belong to both a BlogPost & a Review.
For example, I only want a user to receive a notification if another User has commented on their comment. NOT on a BlostPost - which acts as a commentable and which below each post are the nested comments.
What I've done is the following in my Comment model:
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
has_many :comments, as: :commentable
belongs_to :user, optional: true
acts_as_votable
validates :body, presence: true
def deleted?
user.nil?
end
after_commit :create_notifications, on: :create
private
def create_notifications
Notification.create do |notification|
notification.notify_type = 'commentable'
notification.actor = self.user
notification.user = self.commentable.user
notification.target = self
notification.second_target = self.commentable
end
end
end
And I receive this error:
NoMethodError (undefined method `user' for #<BlogPost:0x00007fc3cab54fd8> Did you mean? super):
app/models/comment.rb:26:in `block in create_notifications' app/models/comment.rb:23:in `create_notifications' app/controllers/comments_controller.rb:17:in `create'
Which makes sense because a BlogPost doesn't belong to a User, so it can't find a user. So it is blocking the ability of a User to add a new comment beneath the BlogPost.
Is there a way I can write an argument like:
if self.commentable = self
notification.user = commentable.user
end
Meaning if the commentable IS a comment and NOT a BlogPost, then the user to receive the notification is the user of the comment (and not incorrectly the BlogPost)
Your tutorial is very clear, but I'm hitting some weeds when I try to then implement this into the nested comments.
Hopefully this is making sense to you, and if not I'm happy to provide a link to my repo if needed.
Thanks for any help that you might be able to provide!
Cheers,
Aaron
(p.s. I've actually followed your previous nested comments tutorial and it is all set up and working great).
☒ ruby object and instantiating it in the comment controller create action
def create
@comment = Comment.new(comment_params)
if @comment.save
CommentNotifier.call(@comment)
...
else
...
end
end
From there, you can make the Comment Notifier have guard clauses, do any checks and pass in any other additional parameters needed. (like passing in the current user to have a guard clause return unless current_user == comment.user) to prevent the current user getting a notification if they reply to their own comment.
Ok, I think I get the logic, but not sure if I've gotten the syntax correct.
def create
@comment = @commentable.comments.new(comment_params)
@comment.user = @current_user
if @comment.save
CommentNotifier.call(@comment)
if @comment = @commentable
else
nil
end
respond_to do |format|
format.html { redirect_to @commentable }
format.js # create.js.erb
end
Basically, if the commentable object is a comment, then the user of that commentable comment SHOULD receive a notification. Else, if commentable is a not a comment (i.e. a BlogPost) they shouldn't!
How would you include the guard clause return unless current_user == comment.user in there as well?
# app/models/comment_notifier.rb
class CommentNotifier
# this is a class method, so it will be called like CommentNotifier.call(@comment, current_user) from your controller
def self.call(commnet, current_user)
new.call(comment, current_user)
end
# initialization stuff for the CommentNotifier instance
attr_accessor :comment, :current_user
def initialize(comment, current_user)
@comment = comment
@current_user = current_user
end
# this is your main method where the business logic will happen.
def call
Rails.logger.info comment
Rails.logger.info current_user
# thanks to attr_accessor, I can simple reference to commant and current_user without the @comment or @current_user
# example guard clauses
return unless current_user
return unless comment
return unless user_same_as_commenter
# do other actions and checks
end
private
# because we create a methods within this class, we can call on these methods since we initialized a new object in the (new.call(comment, current_user))
def user_same_as_commenter
current_user == comment.user
end
end
☒ Thanks, so I just implemented this in a CommentNotifier model as you've written (correcting the typo above in your def self.call at the beginning 'comnet' to 'comment' and here is how the create method looks in my Comments Controller...
def create
@comment = @commentable.comments.new(comment_params)
@comment.user = @current_user
if @comment.save
CommentNotifier.call(comment, current_user)
respond_to do |format|
format.html { redirect_to @commentable }
format.js # create.js.erb
end
else
redirect_to cookies[:original_referrer]
flash[:error] = "Comment could not be created."
end
end
But I receive this error
NameError (undefined local variable or method `comment' for #<BlogPosts::CommentsController:0x00007fd7bfe4c818> Did you mean? @comment):
which is puzzling because it seems to point to my namespaced BlogPosts::CommentsController < CommentsController
class BlogPosts::CommentsController < CommentsController
before_action :set_commentable
def new
@comment = Comment.new(commentable: @commentable)
end
private
def set_commentable
@commentable = BlogPost.friendly.find(params[:blog_post_id])
end
end
I'll be happy to send you a link to my GitHub repo if that will help provide more insight!
The notifications counter updates perfectly in my app layout view, but when I try to click on the notifications url I keep receiving a 500 NameError and it's parsing through and fussing with a link to root_path in my app/layout which doesn't make sense, to me as I know that works fine.
in my comment.rb model
in my views/notifications/_commentable.html.erb
Do you have any ideas as to what might be creating the fuss?
Thanks a lot!
Aaron
The error is :
Showing timesink/app/views/layouts/application.html.erb where line #25 raised:
But now I'm left with this error, which I was receiving before too at some point.
It's with the line in my views/notifications/_notification.html.erb
Any idea what causes this? It's confounding me!
I've been having trouble continuing to set this up with my nested Comments which belong to both a BlogPost & a Review.
For example, I only want a user to receive a notification if another User has commented on their comment. NOT on a BlostPost - which acts as a commentable and which below each post are the nested comments.
What I've done is the following in my Comment model:
And I receive this error:
Which makes sense because a BlogPost doesn't belong to a User, so it can't find a user. So it is blocking the ability of a User to add a new comment beneath the BlogPost.
Is there a way I can write an argument like:
Meaning if the commentable IS a comment and NOT a BlogPost, then the user to receive the notification is the user of the comment (and not incorrectly the BlogPost)
Your tutorial is very clear, but I'm hitting some weeds when I try to then implement this into the nested comments.
Hopefully this is making sense to you, and if not I'm happy to provide a link to my repo if needed.
Thanks for any help that you might be able to provide!
Cheers,
Aaron
(p.s. I've actually followed your previous nested comments tutorial and it is all set up and working great).
From there, you can make the Comment Notifier have guard clauses, do any checks and pass in any other additional parameters needed. (like passing in the current user to have a guard clause return unless current_user == comment.user) to prevent the current user getting a notification if they reply to their own comment.
Basically, if the commentable object is a comment, then the user of that commentable comment SHOULD receive a notification.
Else, if commentable is a not a comment (i.e. a BlogPost) they shouldn't!
How would you include the guard clause return unless current_user == comment.user in there as well?
Thanks David!
Where would you create the Ruby class for CommentNotifier? I'm not sure if I'm following you.
Thanks David!
In my Comment model ...
But I receive this error
which is puzzling because it seems to point to my namespaced BlogPosts::CommentsController < CommentsController
I'll be happy to send you a link to my GitHub repo if that will help provide more insight!
Thanks for your time and consideration...
If I enter this then I get this error...