All threads / In-App Navbar Notifications - GoRails
Ask A Question


You’re not receiving notifications from this thread.

In-App Navbar Notifications - GoRails

Chris Oliver asked in General

Hi Chris,

Thank you for that screencast, it was very clear and useful.

Also I am pleased to learn using UJS from this example. So now when I'm trying to realise some similar function I'm bumping into strange issue that I can't resolve. Maybe you can advise something on this? Probably nothing special here, but I'm not successful in finding solution.

Here is the thing. I'm using that same approach as you proposed, sending GET request to the controller to render JSON file and then use it's data to fill in the markup.

url: "/show_proposals"
dataType: "JSON"
method: "GET"
success: @showProposals

Then in @showProposals method I'm not only inserting data, but also generate references to listen this item:

showProposals: (data) => (
proposers = $.map data.proposers, (proposer) ->

Then I need to handle click and send special request to the same controller to render new JSON and repeat the same procedure from @showProposals:

$(document).on('click', '.proposal-link', ( (event) ->
link = $(this).data("url")
url: link
dataType: "JSON"
method: "GET"
success: @showProposals

So here I have a strange problem. Everything works fine, link is received from element, request sent to the same controller, JSON file rendered and ajax returns success. But whatever I try, it DOES NOT call @showProposals method again.

Do you have any ideas what could it be? Maybe I should make another references to listen rendered items?

Thank you in advance!!

Jordano Moscoso ·

Chris i have a Question!, You have specified the user have many notifications foreign_key as "recipient_id" so why don't you did the same for the "actor_id"? Because que actor doesn't want a notification about himself of course but is it posible to have this?
have_many :notifications, foreign_key: :recipient_id
have_many :notifications, foreign_key: :actor_id


A couple reasons:

1. You only should see notifications where you are the recipient.
2. The actor doesn't matter, it's just some other person that took an action that you should be notified of. You don't really care about the actor, so you can pretty much ignore that relationship which means you don't need to define that second set of notifications. It's a relationship that could exist, but since you will probably never use it, we don't define it.
3. A little unrelated, but you'd also need to change the name of the second "has_many :notifications" because that would override the first one.

Pallavi Hegde ·

hey chris,
I am getting template missing when i try notifications.json , Can u please let me know if i have to add any respond to block in the controller?

No, you don't need to because it will check for the action's json.jbuilder template. Just make sure you created that file and it's index.json.jbuilder in the app/views/notifications folder.

Pallavi Hegde ·

That didnt work for me dont know why, i added a respond to json and now its working fine . Now it shows the notifications in json format, but the url is missing from the array. why is that?
Btw, Thanks for such a quick reply :)

I'm not sure, but compare your code against the source code of mine that's linked in the episode notes. I'm sure you'll figure it out! :)

Pallavi Hegde ·

okay will check that , thanks :)

Pallavi Hegde ·

hey chris, I have given two dummy notifications in my markup but the @notification.length always shows 0 . why is that?

Pallavi Hegde ·

hey chris , i got it working . Thanks for the detailed explanation.
Have a question, how can i add multiple recipients here depending on the role?

I would just create one notification record for each user you want to send it to. You need to do that anyways so you can mark when each user read their notification if you want to add that feature in the future. So normally you would just do a loop, find all the users you want to notify, and then create a Notification record for each user.

Pallavi Hegde ·

ya got it working :) Thank you :)

Hey Chris, I've set up Notifications for 3 models. How do you configure the json.url to route to the right url?

Join the discussion

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

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

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

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.