[RUBY] [Rails] Implementation of "notify notification in some way"

I tried the notification function, but for some reason it didn't work. __

Notification Notification

Image from Gyazo ↑ If there is an unread notification like this, how to implement something like adding a red dot without using javascript or the action of the helper file.

If you haven't implemented the notification function itself yet, let's implement it by referring to the following easy-to-understand articles!

For those who have the same problems as me, such as the challenge in the above article, but it doesn't work.

Define variables in the controller action associated with the view you want to display

events_controller.rb


  def index
    @notification = Notification.all
  end

Get information in the notifications table.

[Easy] Just add a description to html.erb

event:index.html.erb


  <div class="alert-header">
      <%= link_to notifications_path do %>
        <span class="material-icons" id="alert">notification_important</span> 
        <% @notification.each do |notification| %>
          <% if (notification.visited_id == current_user.id) && notification.checked == false %>
            <span class="material-icons" id="on-alert">fiber_manual_record</span>
            <% break %>
          <% end %>
        <% end %>
      <% end %>
    </div>

■ Explanation

<% @notification.each do |notification| %>

Use the each method to process the @notification variable defined in the controller for each notification. The block variable is notification this time, but anything is OK.

<% if (notification.visited_id == current_user.id) && notification.checked == false %>
    <span class="material-icons">fiber_manual_record</span>
    <% break %>
<% end %>

Using the if conditional expression, visited_id (id of the commented user) is current_user.id (id of the currently logged in user), Moreover, if the value of notification_checked (checked column of notifications table) is false (unread), the following processing is performed.

Note that if you do not set it to break, the process will be repeated as many times as there are notifications.

that's all. No matter how many times I tried it, it didn't work, so I arranged it in my own way.

Remarks

current_user.id is a helper method that can be used by introducing gem'devise' to get the signed-in user, but it is OK if you can get the ID of the logged-in user.

__ Please refer to the following article for the introduction of material icons __

Recommended Posts

[Rails] Implementation of "notify notification in some way"
[Rails] Implementation of retweet function in SNS application
Rails implementation of ajax removal
[Rails 6] Implementation of search function
Implementation of gzip in java
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
Implementation of tri-tree in Java
Implementation of HashMap in kotlin
Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of user logic deletion
[Rails] Implementation of CSV import function
[Rails] Asynchronous implementation of like function
[rails] Login screen implementation in devise
[Rails] Implementation of image preview function
Implementation of ls command in Ruby
[Rails] Implementation of user withdrawal function
[Rails] Implementation of CSV export function
[Rails] Implementation of many-to-many category functions
Implementation of like function in Java
[Rails] Implementation of new registration function in wizard format using devise
Implementation of DBlayer in Java (RDB, MySQL)
The identity of params [: id] in rails
Implementation of multi-tenant asynchronous processing in Tomcat
[rails] List of actions defined in Controller
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Rails] Implementation of validation that maintains uniqueness
[Vue.js] Implementation of menu function Vue.js introduction rails6
[Rails] Implementation of search function using gem's ransack
[Order method] Set the order of data in Rails
Implementation of Ruby on Rails login function (Session)
Easy way to create an implementation of java.util.stream.Stream
[Rails] Implementation of image enlargement function using lightbox2
Implementation of digit grouping in flea market apps
SKStoreReviewController implementation memo in Swift UI of iOS14
Recommendation of Service class in Ruby on Rails
One way to redirect_to with parameters in rails
[Rails] Implementation of batch processing using whenever (gem)
How to use JQuery in js.erb of Rails6
[Rails] Implementation of PV number ranking using impressionist
[Rails] Implementation of image slide show using Bootstrap 3
Simple notification function in Rails (only when followed)
[Rails] Ranking and pagination in order of likes
Implementation of GKAccessPoint
Group_by in Rails
[Rails] Notification function
One case of solving a migration error in Rails
Some subtly different repetitive writing styles in Rails View
[Rails] Implementation of drag and drop function (with effect)
Implementation of asynchronous processing for single tenant in Tomcat
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
Calendar implementation and conditional branching in Rails Gem simple calendar
SSL in the local environment of Docker / Rails / puma
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
Arrange posts in order of likes on Rails (ranking)