#136 User Notifications


Using the Notifications Rails Engine, we are able to add notifications to our applications based on certain events. The engine provides the schema and necessary controls and associations to seamlessly integrate into your app.
rails notifications engine 13:08


Terminalrails g notifications:install
rails db:migrate

rails g notifications:controllers
rails g notifications:views
Gemfilegem 'notifications'
layouts/application.html.erb<div class='container'>
  <ul class='navigation'>
  <% if user_signed_in? %>
    <li><%= link_to pluralize(Notification.unread_count(current_user), 'Notification'), notifications_path %></li>
    <li><%= link_to current_user.email, main_app.edit_user_registration_path %></li>
    <li><%= link_to 'Logout', main_app.destroy_user_session_path, method: :delete %></li>
  <% else %>
    <li><%= link_to 'Login', main_app.new_user_session_path %></li>
  <% end %>
  <%= yield %>
models/comment.rbclass Comment < ApplicationRecord
  belongs_to :post
  belongs_to :user

  after_commit :create_notifications, on: :create


  def create_notifications
    Notification.create do |notification|
      notification.notify_type = 'post'
      notification.actor = self.user
      notification.user = self.post.user
      notification.target = self
      notification.second_target = self.post
views/notifications/_post.html.erb<div class=''>
  <%= link_to notification.actor.email, main_app.user_path(notification.actor) %> has commented in
  <%= link_to notification.second_target.content, main_app.user_post_path(notification.user, notification.second_target) %>

<div class=''>
  <%= notification.target.content %>
db/routes.rb  mount Notifications::Engine => "/notifications"

RailsCod3rFuture said over 1 year ago on User Notifications :

If its not too much to ask. Can you please make a similar video for activity_notification gem? Its almost identical to this one, but it may be more optimized. https://github.com/simukappu/activity_notification

kobaltz PRO said over 1 year ago on User Notifications :

I've had a chance to look at this gem. Its documentation is pretty extensive which is always nice to see. I'll consider covering this gem in the future. One thing that I highly dislike about this gem is that it seems a lot more invasive in the models. It also has a lot of added complexity based on the documentation and I'm not convinced that it's necessary to accomplish what they're doing. Overall the gem does look pretty cool and for a large complex app, this may be a good fit.

blimey85 said over 1 year ago on User Notifications :

Second vote for the activity_notification. It looks very slick and something I'm gonna add in to my project soon.

Login to Comment