3

I am trying to get notifications to work in my app. I found "noticed gem" from this GitHub repo and followed all the steps that he does. I have the gem in my gem file, I did bundle install and update and rails db:migrate and everything. However when I try running this in rails console

CommentNotification.with(post: @post).deliver(current_user)

I get

 Traceback (most recent call last):
    1: from (irb):1
NameError (uninitialized constant CommentNotification)

This is my comment_notification.rb class that gets generated under app/notifications/comment_notificaiton.rb when I run rails generate noticed:notification CommentNotification just as he does in the video and just as the documentation suggests.

# To deliver this notification:
#
CommentNotification.with(post: @post).deliver_later(current_user)
CommentNotification.with(post: @post).deliver(current_user)

class CommentNotification < Noticed::Base
  # Add your delivery methods
  #
  deliver_by :database
  # deliver_by :email, mailer: "UserMailer"
  # deliver_by :slack
  # deliver_by :custom, class: "MyDeliveryMethod"

  # Add required params
  #
  param :post

  # Define helper methods to make rendering easier.
  #
  def message
    t(".message")
  end
  #
  def url
    post_path(params[:post])
  end
end
mavenHawk
  • 51
  • 5

3 Answers3

1

You have to restart your spring server.

Using bin/spring stop command, the spring server will be stopped. Then the server will be started using rails server or rails s.

Debanjan Dey
  • 115
  • 2
  • 15
1

A bit late to this, but manually loading the notification class in rails console solved this issue in my case, i.e.: load "app/notifications/comment_notification.rb". (PS: I would also check the spelling of the file name, i.e. comment_notification vs comment_notificaiton)

Zengetsu
  • 109
  • 1
  • 8
-1

Uncomment the first two lines:

# CommentNotification.with(post: @post).deliver_later(current_user)
# CommentNotification.with(post: @post).deliver(current_user)

remember, @post must be the resource that you are going to store in user notifications

user.notifications

@post must exist

Samuel Da Costa
  • 415
  • 3
  • 17