azback5
Joined 10/27/2020
azback5 said over 3 years ago on User Notifications :
No, I'm clicking on the notifications_path  in my app/layout view
  <%= link_to pluralize(Notification.unread_count(@current_user), 'Notification'), notifications_path %>

The error is :

Showing timesink/app/views/layouts/application.html.erb where line #25 raised:

undefined local variable or method `root_path' for #<#<Class:0x00007fb4d1e8b498>:0x00007fb4d91abd38>
Did you mean?  font_path


azback5 said over 3 years ago on User Notifications :
That worked!

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

<%= render partial: "/notifications/#{notification.notify_type}", locals: { notification: notification } %>

timesink/app/views/notifications/notifications/_notification.html.erb where line #11 raised:issing partial notifications/_comment with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :arb, :jbuilder]}. Searched in:
  * "/Users/aaronzomback/Sites/timesink/app/views"
  * "/Users/aaronzomback/.rvm/gems/ruby-2.7.0/gems/notifications-1.1.0/app/views"
  * "/Users/aaronzomback/.rvm/gems/ruby-2.7.0/gems/devise-4.7.2/app/views"
  * "/Users/aaronzomback/.rvm/gems/ruby-2.7.0/gems/activeadmin-2.7.0/app/views"
  * "/Users/aaronzomback/.rvm/gems/ruby-2.7.0/gems/kaminari-core-1.2.1/app/views"
  * "/Users/aaronzomback/.rvm/gems/ruby-2.7.0/gems/form_slider-0.0.5/app/views"
  * "/Users/aaronzomback/.rvm/gems/ruby-2.7.0/gems/actiontext-6.0.3.2/app/views"
  * "/Users/aaronzomback/.rvm/gems/ruby-2.7.0/gems/actionmailbox-6.0.3.2/app/views"

Any idea what causes this? It's confounding me!

azback5 said over 3 years ago on User Notifications :
David, thanks for all of your help. 

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).

azback5 said over 3 years ago on User Notifications :
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?

Thanks David!

azback5 said over 3 years ago on User Notifications :
  I actually didn't create a Ruby Class for it, I just assumed that it was a native feature of the Notifications gem...

Where would you create the Ruby class for CommentNotifier? I'm not sure if I'm following you.