Ask A Question

Notifications

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.

$.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!!

Reply
Jordano Moscoso 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

Thanks.

Reply

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.

Reply
Pallavi Hegde 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?

Reply

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.

Reply
Pallavi Hegde Pallavi Hegde

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

Reply
Pallavi Hegde 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?

Reply

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.

Reply

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

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.