Skip to main content

In-App Navbar Notifications - GoRails

General • Asked by Chris Oliver

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.

$.ajax(
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) ->
"<li>#{proposer.name}</li>"
)

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) ->
event.preventDefault()
link = $(this).data("url")
$.ajax(
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!!


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

Thanks.

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.


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.

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! :)

okay will check that , thanks :)


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


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.

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?


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 22,346+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.