[Ruby on Rails] DM, chat function

Target

chat.gif

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

Premise

-Build login environment with devise --User details screen, list and created

flow

Creating 1 model 2 Edit model 3 Creating a controller 4 Editing routing 5 Edit view

Creating a model

Terminal


$ rails g model Room

$ rails g model Chat user_id:integer room_id:integer message:string

$ rails g model UserRoom user_id:integer room_id:integer

$ rails db:migrate
Supplement 1 [room] A room where users can talk to each other.
Supplement 2 [chat] A table that stores what the user says.
Supplement 3 [user_room] Manage this as an intermediate table due to the many-to-many relationship between user and room.

Edit model

We will make the following relations. スクリーンショット 2020-09-10 22.00.12.png

app/models/room.rb


  has_many :user_rooms
  has_many :chats
Supplement [Relation] There are many user_rooms in the room, so one-to-many. There are many chats in the room, so one-to-many.

app/chats/room.rb


  belongs_to :user
  belongs_to :room

app/user_rooms/room.rb


  belongs_to :user
  belongs_to :room

app/models/user.rb


  has_many :user_rooms
  has_many :chats
Supplement [Relation] One-to-many because one user owns many user_rooms. One-to-many because one user does many chats.

Creating a controller

Terminal


$ rails g controller chats

app/controllers/chats_controller.rb



class ChatsController < ApplicationController
  def show
    @user = User.find(params[:id])
    rooms = current_user.user_rooms.pluck(:room_id)
    user_rooms = UserRoom.find_by(user_id: @user.id, room_id: rooms)

    unless user_rooms.nil?
      @room = user_rooms.room
    else
      @room = Room.new
      @room.save
      UserRoom.create(user_id: current_user.id, room_id: @room.id)
      UserRoom.create(user_id: @user.id, room_id: @room.id)
    end
    @chats = @room.chats
    @chat = Chat.new(room_id: @room.id)
  end
  def create
    @chat = current_user.chats.new(chat_params)
    @chat.save
    redirect_to request.referer
  end

  private
  def chat_params
    params.require(:chat).permit(:message, :room_id)
  end
end

In the comment out below, supplement the difficult parts.

app/controllers/chats_controller.rb


    def show
      #Get which user to chat with.
      @user = User.find(params[:id])
      
      #Current user user_room in room_Assign an array of id values to rooms.
      rooms = current_user.user_rooms.pluck(:room_id)

      # user_From the room model
      # user_If the id matches the id of the chat partner,
      # room_Records whose id matches any of the above rooms
      # user_Substitute in rooms.
      user_rooms = UserRoom.find_by(user_id: @user.id, room_id: rooms)
  
      #If user_If room is not empty
      unless user_rooms.nil?
        # @Above user in room_Substitute room for room
        @room = user_rooms.room
      else
        #Other than that, create a new room
        @room = Room.new
        @room.save
        # user_Create room for current user and chat partner
        UserRoom.create(user_id: current_user.id, room_id: @room.id)
        UserRoom.create(user_id: @user.id, room_id: @room.id)
      end
      @chats = @room.chats
      @chat = Chat.new(room_id: @room.id)
    end

Edit routing

config/routes.rb


  get 'chat/:id' => 'chats#show', as: 'chat'
  resources :chats, only: [:create]

edit view

erb:app/view/users/show.html.erb


<% if current_user != @user %>
  <%= link_to 'Start chat', chat_path(@user.id)%>
<% end %>

erb:app/view/chats/show.html.erb


<%= form_with model: @chat do |f| %>
    <%= f.text_field :message %>
    <%= f.hidden_field :room_id %>
    <%= f.submit %>
<% end %>
<table>
    <thead>
        <tr>
            <td>Posted by name</td>
            <td>Posted content</td>
        </tr>
    </thead>
    <tbody>
        <% @chats.each do |chat| %>
            <tr>
                <td><%= chat.user.name %></td>
                <td><%= chat.message %></td>
            </tr>
        <% end %>
    </tbody>
</table>
Supplement [f.hidden_field] Although f.hidden_field is not displayed, it is used when sending what you want to send as params.

reference

-I tried to explain the complicated DM function in Rails in detail with 10,000 characters -About pluck method and ids method

Recommended Posts