
ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina
-Build login environment with devise --User details screen, list and created
Creating 1 model 2 Edit model 3 Creating a controller 4 Editing routing 5 Edit view
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
 We will make the following relations.

app/models/room.rb
  has_many :user_rooms
  has_many :chats
 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
 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
config/routes.rb
  get 'chat/:id' => 'chats#show', as: 'chat'
  resources :chats, only: [:create]
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>
 -I tried to explain the complicated DM function in Rails in detail with 10,000 characters -About pluck method and ids method
Recommended Posts