Ask A Question

Notifications

You’re not receiving notifications from this thread.

In-App Navbar Notifications Discussion

Is there another way than checking the db every few seconds? Sounds like it could be taxing. Also I implemented this in a web app I am working on that uses React on the front end (via react rails) and there seems to be a few second delay before the notification appears. I guess moving the logic from jQuery into React itself may help. I was just wondering if there are any other ways. Thank you.

Reply

I have noticed this same issue. The bell itself doesn't appear for about 2 seconds, and then 3 seconds later, the notification number appears... If anyone has a fix, that would be great.

Reply

I have followed the steps in the tutorial, however, when i check the rails console, no notification is not being created when i create a comment (forum_thread) on a post (forum_post).

Reply

Thanks for this course Chris. I'm trying to implement this to an app am building (I want to create the notifications for comments where the comment belongs to episode and episode belongs to story and genre)... When i checked out Notification.all in my console it outputted it but when i navigated to http://localhost:3000/notifications.json i got an error telling me that "undefined method `episode_path' for #<#<class:0x157f1538>:0x15149e68>"

Here is my index.json.jbuilder
https://uploads.disquscdn.c...

Reply

i'm having this error "undefined method `underscore' for nil:NilClass" , i can't quite figure it out , Error happen ,when rendering the json template

json.template render partial: "notifications/#{notification.notifiable_type.underscore.pluralize}/#{notification.action}", locals: {notification: notification}, formats: [:html]

Reply
I did find myself having issues with the jQuery map method in the handleSuccess using Rails 5.1 (with jQuery). Instead I found the Ruby map works just fine:

items = data.map (notification) ->
  "<li><a class='dropdown-item' href='#{notification.url}'>'#{notification.sender} #{notification.action} a #{notification.notifiable.type}'</a></li>"


Reply
Hey Chris, I was wondering how you would tackle the notification type in cases where the text you want to render isn't exactly the class name and there's more text that depends on the notifiable object. For example, let's say I want to have the following two notifications:

  • "John D started following you"
  • "Jane D sent you a friend request"

I did a quick test and added a `notification_to_s` method to my `Follow` and `FriendRequest` models like so:

# follow.rb
  def notification_to_s
    "following you"
  end

# friend_request.rb
  def notification_to_s
    "you a friend request"
  end

And then adapted the jbuilder method like this:

# index.json.jbuilder
# ...
  json.notifiable do
    json.type notification.notifiable.notification_to_s
  end

This doesn't seem as dynamic as your example. Can you think of a better way to do this?
Reply

Did you end up doing a react version of this? I am having a hard time integrating my redux module into the rails header.

Reply

Hi Chris!

I am trying to implement the notification feature and it works perfectly up to the point where I try to fetch the @notifications div in notifications.js.coffee file.

class Notifications
  constructor: ->
    @notifications = $("[data-behavior='notifications']")
    @setup()

  setup: ->
    console.log(@notifications)

  jQuery ->
    new Notifications

I know that the file loads because if I put an alert message it executes on page load. But when I do the code above it dont get anything consoled..

Reply

Hi Chris!

The notifications are working. What you call threads, we call posts, and what you call posts, we call comments.

Two issues:

1) It only works when the json.url line is commented out:

json.url post_path(notification.notifiable.post, anchor: dom_id(notification.notifiable)) 

As soon I uncomment, notifications totally stop working--the number disappears from the bell, and the notification messages themselves also disappear. Once I comment out the line again, the bell numbers return, and so do the notifications. Thoughts on how to get that to work? We definitely would like each notification to be linkable to that particular event.

2) we are showing messages for both posts (post_path) and also comments (comment_path). But how will index.json.builder know when to follow the post_path, and when to follow the comment_path? And then again, once we add other notifiable events, like reaching a higher level, etc.?

Thanks very much!
-Monroe

Reply

If it works commented out, then you're probably throwing an error on that line. Read the logs to debug it. :)

Reply

Okay! I'll try that. :D

Reply

This is the error in the rails console:

https://gyazo.com/c14ed47cb5fbe8dbf3cb7158c4865407

Does this mean I forgot to create a post method somewhere?

Reply

No, it says you tried to call the "post" method on a Post object. It's not a Comment, it's a Post.

Reply

Hi Chris, I'm learning alot from this tutorial. I really appreciate the throughness that you have for this tutorial. I am however, facing an issue where I am unable to retrieve the jQuery from the index page. My console is unable to detect anything from the console.log.

Could you advise me what would be the possible scenario where I made a mistake?

Reply

Joseph, I had a similar issue, and solved it by adding a '//= require_tree' line at the application.js.

Reply

Having trouble implementing it with Bulma. Any ideas?

Reply

Hi,

this is working good for me. but in other localhost applications also calling /notifications.json.
And getting error ActionController::RoutingError (No route matches [GET] "/notifications.json").

Reply

It seems to be a problem when following the links on the notifications dropdpwn (actually following all the links in the app). *When following the links the notifications feature doesn't work anymore :( Only on refreshing the page! *
Any ideas on solving this issue?
Cheers

Reply

Hey,
Such a really awesome episode but I`m getting error about ServiceWorker. You can see the below mentioned errors:
Failed to register/update a ServiceWorker for scope ‘http://localhost:3000/forum_threads/’: Load failed with status 404 for script ‘http://localhost:3000/serviceworker.js’
TypeError: ServiceWorker script at http://localhost:3000/serviceworker.js for scope http://localhost:3000/forum_threads/ encountered an error during installation.
How can I solve this?

Reply

Is there a way you could group the ntoifications or aggregrate them say for example in an application you would have "John and 3 others commented on your post" versus 3 seperate notifications.

Reply

Yep, although it's a bit tricky. You would probably want to do this in ruby, and loop through each notification to see if the next one is the same as the last and then you can build up those into a group to display.

Been thinking about covering this at some point, but haven't gotten to it yet.

Reply

Hi, thanks so much for the tutorial! Two questions:

1- How can I create a page that displays all notifications, and display a link to this page in the dropdown when data.length >0 or 0? (i.e when there are and when there are not notifications, have a 'show all' item appear in the dropdown that brings you to a page with all your notifications

2- How can I make the json.url path in index.json.jbuilder dynamic for different types of notifications? i.e :
json.url users_friend_request_path(notification.notifiable) is the url for when the notification is a friend request, and I've tried string interpolation using notifiable.type and a few other things that did not work.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.