Rails 5 Actioncable Real-Time Notifications – Part 2 Styling

We are continuing building our Facebook style Notifications using Rails 5 ActionCable. Please check Part 1 for basic setup, please also check Live Demo if you want to see the final result. In this part we will cover how to style our notification center.

facebook like notifications rails5

Anatoly Spektor

Let me help you!

Hey, I am Anatoly, founder of this blog. I would love to help you! I have 6+ years experience in Web Development and IT Leadership. Lets work together on your project! Leave me your email and I will schedule a FREE Consultation!

Part 1 is available here.

Source code is available here.

Styling

One of the design parts here is bell  icon with a counter.
Lets create them.

We will need couple of things to make it work.

Firstly, Icon of a Bell in *.SVG – Download (alternative_link)  and save it as “notification_bell”. Then  put image into notificator/app/assets/images/icons/notification_bell.svg (create icons folder).
Secondly, we need to put actual counter on the page. For this purpose lets create another partial:

notificator/app/views/events/_counter.html.erb

As you might have noticed, this partial show icon of a bell, that we downloaded, and also another variable – counter. This variable is a number of notifications.

Lets include counter in our notification center:

notificator/app/views/notifications/_notification_center.html.erb

Lines 2-4 is new, I have included counter  and passed local variable counter that counts our notifications.

Nice work! Time to do some styling!

Lets put following CSS in our notification.scss:

notificator/app/assets/stylesheets/notifications.scss

This does two things: positions our notification centre to the left and styles counter number and counter icon. Pretty straightforward.

Result should look like this:

rails5_actioncable_tutorial_screen3

We are slowly getting there!

Next stop, I have some specific CSS for notification center, just to style it properly. I won’t get into much details of it, but if you have questions why something is the way it is, please put your questions into comments and lets discuss.

Please append following CSS to your notifications.scss

notificator/app/assets/stylesheets/notifications.scss

Good job! To finish it up, we need some JavaScript that opens up and closes notification center. I am using JQuery to make it work.

notificator/app/assets/javascripts/notifications_center.js

If you did everything correctly, you should be able to refresh the page click on icon and get something like this:

rails5 actioncable tutorial screen4 notification center

If you click again it should close the notification center. Also if you go to http://0.0.0.0:3000/messages/new and add some more messages and refresh the page, your counter will update and your notification will appear in the list.

This is all cool, but wouldn’t it be much cooler, if you did not have to refresh the page ?

That is for this part.

Lets go to the Part 3 and  see how to make those notification update in real-time using ActionCable.

Rails 5 Actioncable Real-Time Notifications – Part 3 Stream Notifications

Please post your questions into comment section.

If you like what you see please Subscribe to my blog. If you need help with your project, please Get In Touch.

Anatoly Spektor

IT Consultant with 6 years experience in Software Development and IT Leadership. Participated in such projects as Eclipse IDE, Big Blue Button, Toronto 2015 Panam Games.

Related Posts

Join the Discussion

Your email address will not be published. Required fields are marked *

Discussion

  1. Moe

    Very well done Tutorial!
    Many of your Tutorials helped me through the nights :)
    Thank you very much for puttig so much work in it!
    When will Part 3 be released?

    1. Anatoly Spektor

      Part 3 is already available, glad you like it :)

arrow