質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

0回答

280閲覧

掲載ごとにDMを作成したい

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2022/09/29 12:00

編集2022/09/29 12:22

困っている点

DM機能を作成したのですが、違う掲載でも同じ掲載者の場合、最初に作成されたDMする部屋(room)が使用される使用に今なっています。
(現在)例 掲載者A → 掲載1(使用room_id: 1)  ← 応募  応募者B
→ 掲載2(使用room_id: 1) ← 応募

(希望)例 掲載者A → 掲載1(使用room_id: 1)  ← 応募  応募者B
→ 掲載2(使用room_id: 2) ← 応募
→ 掲載3(使用room_id:3) ← 応募

現在、あらかじめ応募され、roomが存在するときはメッセージへというボタンになっているが最新の掲載でも、以前応募した同じ掲載者の場合は、メッセージへボタンが表示されるので、掲載ごとにroomを作成してメッセージのやり取りをできるようにしたいです。

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 @user = User.find(params[:id]) @plan = Plan.find(params[:id]) @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

views/plans/show.html.erb

<% if user_signed_in? %> <% if current_user == @plan.user %> <div class='plan-content'> <h4><%= @plan.title %></h4> <h5><%= link_to @plan.user.name, user_path(@plan.user)%></h5> <p><%= @plan.start_on.strftime('%Y年%m月%d日') %>~<%= @plan.end_on.strftime('%Y年%m月%d日') %></p> <p><%= image_tag @plan.photo.url, :alt => '企画書イメージ図' if @plan.photo? %></p> <p><%= @plan.detail %></p> <div class= "address-content"> <h5>希望条件</h5> <p><%= @plan.conditions %></p> </div> <div class= "number-content"> <h5>電話番号</h5> <p><%= @plan.user.telephone_number %></p> </div> <div class= "address-content"> <h5>住所</h5> <p><%= @plan.user.address %></p> </div> <div class="plan-content-btn"> <%= link_to "削除", @plan, method: :delete, data: { confirm: "削除しますか?" } %> <%= link_to '戻る', '/' %> </div> </div> <% else %> <div class='plan-content'> <h4><%= @plan.title %></h4> <h5><%= link_to @plan.user.name, user_path(@plan.user)%></h5> <p><%= @plan.start_on.strftime('%Y年%m月%d日') %>~<%= @plan.end_on.strftime('%Y年%m月%d日') %></p> <p><%= image_tag @plan.photo.url, :alt => '企画書イメージ図' if @plan.photo? %></p> <p><%= @plan.detail %></p> <div class= "address-content"> <h5>希望条件</h5> <p><%= @plan.conditions %></p> </div> <div class= "number-content"> <h5>電話番号</h5> <p><%= @plan.user.telephone_number %></p> </div> <div class= "address-content"> <h5>住所</h5> <p><%= @plan.user.address %></p> </div> <div class="plan-content-btn"> <p><%= render 'favorites/favorite_button', plan: @plan %></p> <div class="plan-content-btn"> <% unless @plan.user.id == current_user.id %> <% if @isRoom == true %> <p class="user-show-room"><a href="/rooms/<%= @roomId %>" class="btn btn-primary btn-lg">メッセージ</a> <% else %> <%= form_for @room do |f| %> <%= fields_for @entry do |e| %> <%= e.hidden_field :user_id, value: @plan.user.id %> <% end %> <%= f.submit "応募", class:"btn btn-primary btn-lg user-show-chat"%> <% end %> <% end %> <% end %> <%= link_to '戻る', '/', class:"btn btn-primary btn-lg " %> </div> </div> </div> <% end %> <% else %> <div class='plan-content'> <h4><%= @plan.title %></h4> <h5><%= link_to @plan.user.name, user_path(@plan.user)%></h5> <p><%= @plan.start_on.strftime('%Y年%m月%d日') %>~<%= @plan.end_on.strftime('%Y年%m月%d日') %></p> <p><%= image_tag @plan.photo.url, :alt => '企画書イメージ図' if @plan.photo? %></p> <p><%= @plan.detail %></p> <div class= "address-content"> <h5>希望条件</h5> <p><%= @plan.conditions %></p> </div> <div class= "number-content"> <h5>電話番号</h5> <p><%= @plan.user.telephone_number %></p> </div> <div class= "address-content"> <h5>住所</h5> <p><%= @plan.user.address %></p> </div> <div class="plan-content-btn"> <%= link_to '応募する', new_user_session_path , class:"btn btn-primary user-show-chat" %> <%= link_to '戻る', '/' %> </div> </div> <% end %>

plans_controller

class PlansController < ApplicationController before_action :set_plan, only: [:show, :edit, :update, :destroy ] before_action :require_user_logged_in before_action :current_user, only: [:destroy, :edit, :new] before_action :require_user_logged_in, only: [:edit] before_action :plan_destroy, only: [:show, :index] def index @pagy, @plans = pagy(Plan.order(id: :desc)) end 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 def new @plan = Plan.new if @plan.user == current_user redirect_to '/' end end def create @plan = current_user.plans.build(plan_params) if @plan.save flash[:success] = '企画掲載を設定しました' redirect_to root_url else @pagy, @plans = pagy(current_user.plans.order(id: :desc)) flash.now[:danger] = '企画掲載に失敗しました' render 'plans/new' end end def destroy @plan = Plan.find_by(id: params[:id]) @plan.destroy flash[:success] = '掲載を中止しました。' redirect_to("/") end private def set_plan @plan = Plan.find(params[:id]) end def plan_params params.require(:plan).permit(:title, :photo, :detail, :start_on, :end_on, :conditions) end def plan_destroy Plan.where('end_on < ?', Date.today).destroy_all end def correct_user @plan = current_user.plans.find_by(id: params[:id]) unless @plan redirect_to root_url end end end

拙い質問で誠に申し訳ありません。
DMの部屋(room)を掲載(plan)ごとに作成する振り分け方のアドバイスを頂けたら幸いです。

DM機能作成にあたっての参考サイト
https://qiita.com/aaaasahi_17/items/9e7f344488c720aaf116
https://qiita.com/bindingpry/items/6790c91f374acc25bea2

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問