投稿ごとにルームを作り、投稿の名前をルームの名前にしたい

困っていること

投稿に応募機能を付け、応募ボタンを押すとDMできる機能を設置しました。
ですが、同じ投稿者で違う投稿に応募しても、同じ部屋(room)を使用することになってしまいます。
またルームの名前も、どの投稿に対して応募したかわかるように、応募した投稿の名前をルーム作成と同時に持ってきたいです。
1.ユーザーではなく投稿ごとに投稿者と応募者のルームを作りたい

2.投稿のタイトルをルームの名前に使われるようにしたい。

知りたいこと

1.plans_controllerのshow箇所の@userEntry=Entry.where(user_id: @plan.user.id)を変更させると投稿(plan)ごとにDMの部屋が作成できますでしょうか?

2.rooms_controllerのcreate箇所で投稿のデータを取得してルームテーブルにあるroom_nameカラムに入れるという流れでしょうか?

rooms_controller

class RoomsController < ApplicationController before_action :authenticate_user! def index end def create @room = Room.create @entry1 = Entry.create(room_id: @room.id, user_id: current_user.id) @entry2 = Entry.create(params.require(:entry).permit(:user_id, :room_id).merge(room_id: @room.id)) redirect_to "/rooms/#{@room.id}" end def show @room = Room.find(params[:id]) if Entry.where(user_id: current_user.id,room_id: @room.id).present? @messages = @room.messages @message = Message.new @entries = @room.entries else redirect_back(fallback_location: root_path) end end end

plans_controllerのルーム関連コード

def show @plan=Plan.find(params[:id]) @currentUserEntry=Entry.where(user_id: current_user.id) @userEntry=Entry.where(user_id: @plan.user.id) unless @plan.user.id == current_user.id @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 else @room = Room.new @entry = Entry.new end end end

room.rb

class Room < ApplicationRecord has_many :entries, dependent: :destroy has_many :messages, dependent: :destroy end

entry.rb

class Entry < ApplicationRecord belongs_to :user belongs_to :room end

情報が拙い可能性がありますので、また回答に必要なコードは追々、質問を修正させて提供させていただきます。
どうかよろしくおねがいいたします。

参考サイト
https://qiita.com/aaaasahi_17/items/9e7f344488c720aaf116
https://qiita.com/bindingpry/items/6790c91f374acc25bea2

コメントを投稿

0 コメント