0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

マチアプの作り方

0
Last updated at Posted at 2026-01-08

前提

https://zenn.dev/kazu1/articles/ca4e2286ec821d
この記事でDM機能をつけてきてください

1.usersテーブルにactiveカラムを作る

マイグレーションファイルの中身をいじって、デフォでアクティブカラムにtrueと記入されるようにします

db/migrate/〇〇〇〇〇〇_add_active_to_user.rb
class AddActiveToUser < ActiveRecord::Migration[7.1]
  def change
    add_column :users, :active, :string, null: true, default: "true"
  end
end

アクティブユーザーって?

本来の意味であれば、「Webサイトやアプリなどのサービスを、設定された一定期間内に実際に利用(訪問、ログイン、操作など)したユニークユーザーのこと」なのですが、今回の場合は「アカウント削除していないユーザー」をアクティブユーザーとして扱おうと思います。

2.アクティブ関数を作成する

ユーザーモデルにアクティブカラムの中身がtrueの人を探すための関数を作る

app/models//user.rb
  scope :active, -> { where(active: "true") }

アクティブユーザーの判定の仕方

1の手順でuserテーブルの中にactiveカラムを追加で作成し、activeカラムの中にデフォルトでtrueという文字を入力させました。
2の手順ではactiveカラムの中身がtrueのユーザーを探す関数を作っています。
ユーザーがアカウントを消したらカラムの中身ごと全て消えるので、逆説的にactiveカラムの中身がtrueなユーザー=アクティブユーザーということになりますね。

3.usersコントローラーのshowアクションに指示を追加する

app/controllers//users_controller.rb
      list = User.active.pluck(:id) #←※1
      @url = list.shuffle!.first #←※2

※1
2の手順で作成したアクティブユーザーを出す関数を使ってUserテーブルからアクティブユーザーの情報を全員分取り出し、各ユーザーのidを取り出してlistという文字に代入させています。
※2
listの中に並べられた全てのアクティブユーザーのidをランダムに並び替え、最初の1つ目だけを抽出して@urlに代入します。
これにより@urlを表示するたびにアクティブユーザーのidがランダムに代入されます。

4.matchページをusersのビューに新規作成

app/view/users/match.html.erb
<% if current_user.id == @user.id %>
    近くにいいねできる人がいません
    <% else %>
    <%= @user.name %>
    <%= @user.profile %>
        <% if @isRoom == true %>
            <p><%= link_to 'DMへ', room_path(@roomId) %></p>
        <% else %>
        <%= form_for @room do |f| %>
            <%= fields_for @entry do |e|%>
            <% e.hidden_field :user_id, value: @user.id %>
        <% end %>
        <%= f.submit "メッセージを送る"%>
        <% end %>
    <% end %>
<% end %>
<%= link_to "Tweet一覧に戻る", tweets_path %>

マッチング用の新しいページを作成して、前提の記事で作成したusersのshowページとほぼ同じ内容を貼ります。
https://zenn.dev/goldsaya/articles/c8bc222faac8c0

5.usersコントローラーにmatchアクションを作成

app/controllers/users_controller.rb
    def match
      list = User.active.pluck(:id)
      @url = list.shuffle!.first
      @user=User.find(params[:bangou])
      @currentUserEntry=Entry.where(user_id: current_user.id)
      @userEntry=Entry.where(user_id: @user.id)
        if @user.id == current_user.id
          @msg ="他のユーザーとDMしてみよう!"
        else
        @currentUserEntry.each do |cu|
          @userEntry.each do |u|
            if cu.room_id == u.room_id then
              @isRoom = true
              @roomId = cu.room_id
            end
          end
        end
  
        if @isRoom != true
            @room = Room.new
            @entry = Entry.new
        end
        end
    end

前提の記事で作成したルーム判定やDM用のアクションをコピペして、3の手順で作ったshowアクションへの追記をコピペしましょう。マッチングページから次のマッチングページに飛ぶ時に使います。
https://zenn.dev/goldsaya/articles/c8bc222faac8c0

5.showページにmatchページに飛ぶリンクを作成

app/view/users/show.html.erb
    <%= link_to "マッチングページへ", users_match_path(bangou: @url) %>

4の手順で作成したmatchページへのlink_toを作成します。
このlink_toを表示する際に@urlに代入されているランダムに生成されたアクティブユーザーのidをbangouという名前でusers_match_pathに送ります。

6.マッチングページに飛ぶためのurlをroutes.rbで作成

app/config/routes.rb
  get "users/match/:bangou" => "users#match", as: :users_match 

usersのmatchページに飛ぶためのURLを作成します。5の手順でランダムに選択されたアクティブユーザーのidをbangouという文字に代入しているため、

7.完成!もし要素足したかったらDM一覧ページとか作ってみてもいいかも!

app/view/users/dmitiran.html.erb
DM一覧ページ
<br>
<% @user.entries.each do |t| %>
    <% t.room.entries.each do |e| %>
        <% unless e.user_id == @user.id %>
        プロフ画像後で入れる
        <%= link_to e.user.name, user_path(e.user.id) %>
        <%= link_to 'DMへ', room_path(e.room.id) %><br>
        <% end %>
    <% end %>
<% end %>
app/view/users/show.html.erb
<% if current_user.id == @user.id %>
    <%= link_to "編集する", edit_user_registration_path %>
    <%= link_to 'DM一覧へ', users_dmitiran_path(id: @user.id) %>
    <%= link_to "マッチングページへ", users_match_path(bangou: @url) %>
    <% else %>
        <% if @isRoom == true %>
            <p><%= link_to 'DMへ', room_path(@roomId) %></p>
        <% else %>
        <%= form_for @room do |f| %>
            <%= fields_for @entry do |e|%>
            <% e.hidden_field :user_id, value: @user.id %>
        <% end %>
        <%= f.submit "DMを開始する"%>
        <% end %>
    <% end %>
<% end %>
app/config/routes.rb
  get "users/dmitiran/:id" => "users#dmitiran", as: :users_dmitiran
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?