I tried the notification function, but for some reason it didn't work. __
↑ 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.
events_controller.rb
def index
@notification = Notification.all
end
Get information in the notifications table.
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.
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