[RUBY] Simple notification function in Rails (only when followed)

A simple notification feature in Rails. (Only when followed for the time being)

Routing settings

config/routes.rb


  resources :notifications, only: [:index]

Creating a controller

$ rails g controller notifications

Creating a model

$ rails g model Notification visitor_id:integer visited_id:integer
$ rails db:migrate

Model and association settings

app/models/user.rb


  has_many :active_notifications, class_name: "Notification", foreign_key: "visitor_id", dependent: :destroy
  has_many :passive_notifications, class_name: "Notification", foreign_key: "visited_id", dependent: :destroy

  def create_notification_follow!(current_user)
    temp = Notification.where(["visitor_id = ? and visited_id = ? ",current_user.id, id])
    if temp.blank?
      notification = current_user.active_notifications.new(visited_id: id)
      notification.save if notification.valid?
    end
  end

app/models/notification.rb


  belongs_to :visitor, class_name: 'User', foreign_key: 'visitor_id'
  belongs_to :visited, class_name: 'User', foreign_key: 'visited_id'

Controller settings

app/controllers/relationships_controller.rb


  def create
    @user = User.find(params[:user_id])
    current_user.follow(params[:user_id])
    @user.create_notification_follow!(current_user) #Postscript part
    redirect_to request.referer
  end

app/controllers/notifications_controller.rb


  def index
    @notifications = current_user.passive_notifications.order(created_at: :DESC)
  end

View (notification list) settings

app/views/notifications/index.html


<h1>Notice</h1>
<% if @notifications.exists? %>
  <% @notifications.each do |notification| %>
    <table>
      <tr>
        <td>
          <%= attachment_image_tag notification.visitor, :image, size: "50x50" %>
        </td>
        <td>
          <%= link_to user_path(notification.visitor) do %>
            <%= notification.visitor.name %>
          <% end %>
        </td>
        <td>
Followed by.
        </td>
        <td>
          <%= " (#{time_ago_in_words(notification.created_at)}Before)" %>
        </td>
      </tr>
    </table>
  <% end %>
<% else %>
  <p>There is no news</p>
<% end %>

Recommended Posts

Simple notification function in Rails (only when followed)
Implement simple login function in Rails
[Rails] Notification function
Implement application function in Rails
[Rails DM] Let's create a notification function when DM is sent!
Add a search function in Rails.
Implement CSV download function in Rails
[Rails] A simple way to implement a self-introduction function in your profile
[Rails] Function restrictions in devise (login / logout)
[Rails withdrawal] Create a simple withdrawal function with rails
Implemented follow function in Rails (Ajax communication)
[Rails] Store only checked items in DB
Output Hello World in kotlin's simple main function
Create authentication function in Rails application using devise
Error in bundle install when running rails new
Posting function implemented by asynchronous communication in Rails
Implement star rating function using Raty in Rails6
[Rails] Implementation of retweet function in SNS application
[Rails] Implementation of "notify notification in some way"
[Japanese localization] i18n rails Easy Japanese localization view display only
How to make a follow function in Rails
[Rails 6] Ranking function
[Rails] Category function
Rails follow function
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implement post search function in Rails application (where method)
[Rails] Implement credit card registration / deletion function in PAY.JP
Implement user follow function in Rails (I use Ajax) ②
Regarding overcapacity when setting Rails tutorial environment in Cloud 9
[Ruby on Rails] Post image preview function in refile
I want to define a function in Rails Console
Calendar implementation and conditional branching in Rails Gem simple calendar
Implement user follow function in Rails (I use Ajax) ①
Rails Tutorial Extension: I created a follower notification function