[92]Bootstrap | メッセージと会話 | コントローラとビュー作成<< [ホームに戻る] >> [94]Bootstrap | 通知
「48 | メッセージと会話 | 会話表示」の内容をBootstrapに変更します。
会話表示できるようにします。
「app\controllers」フォルダに「conversations_controller.rb」ファイルを新規作成して下さい。
app\controllers\conversations_controller.rb(新規作成したファイル)
class ConversationsController < ApplicationController before_action :get_conversations def list end def show @user = User.find(params[:id]) @conversation = Conversation.where("(sender_id = ? AND receiver_id = ?) OR (sender_id = ? AND receiver_id = ?)", current_user.id, params[:id], params[:id], current_user.id ).first if !@conversation.present? redirect_to conversations_path, alert: "無効です" else @messages = Message.where(conversation_id: @conversation.id) end end private def get_conversations @conversations = Conversation .where("sender_id = ? OR receiver_id = ?", current_user.id, current_user.id) .order(updated_at: :desc) end end
ルートの設定をします。
「config\routes.rb」ファイルに以下の記述を追加します。
記述追加 config\routes.rb(18,19行目)
get '/conversations', to: 'conversations#list', as: "conversations" get '/conversations/:id', to: 'conversations#show', as: "conversation_detail"
config\routes.rb
Rails.application.routes.draw do root 'pages#home' devise_for :users, path: '', path_names: {sign_up: 'register', sign_in: 'login', edit: 'profile', sign_out: 'logout'}, controllers: {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations'} get 'pages/home' get '/dashboard', to: 'users#dashboard' get '/users/:id', to: 'users#show', as: 'user' get '/your_trips' => 'reservations#your_trips' get '/your_reservations' => 'reservations#your_reservations' get 'search' => 'pages#search' get 'settings/payment', to: 'users#payment', as: 'settings_payment' get 'settings/payout', to: 'users#payout', as: 'settings_payout' get '/conversations', to: 'conversations#list', as: "conversations" get '/conversations/:id', to: 'conversations#show', as: "conversation_detail" post '/users/edit', to: 'users#update' post '/settings/payment', to: 'users#update_payment', as: "update_payment" post 'messages', to: 'messages#create' resources :rooms, except: [:edit] do member do get 'listing' get 'pricing' get 'description' get 'photo_upload' get 'amenities' get 'location' get 'preload' get 'preview' delete :delete_photo post :upload_photo end resources :reservations, only: [:create] end resources :guest_reviews, only: [:create, :destroy] resources :host_reviews, only: [:create, :destroy] resources :reservations, only: [:approve, :decline] do member do post '/approve' => "reservations#approve" post '/decline' => "reservations#decline" end end # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end
ナビゲーションバーにリンクを追加します。
記述追加 app\views\shared\_navbar.html.erb
48行目に「<%= link_to 'メッセージの確認', conversations_path, class: "btn btn-light", data: { turbolinks: false} %>」の記述を追加しています。
<nav class="navbar navbar-expand-lg navbar-dark bg-info" style="z-index: 5;"> <a class="navbar-brand" href="/"><h1 class="navh1">MinpakuBs</h1></a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse justify-content-end" id="navbarNavDropdown"> <ul class="navbar-nav"> <!-- もしログインしていなかったら--> <% if (!user_signed_in?) %> <li class="nav-item" style="margin-right: 20px; margin-bottom: 5px;"> <%= link_to "新規ユーザ登録", new_user_registration_path, class: "btn btn-light" %> </li> <li class="nav-item"> <%= link_to "ログイン", new_user_session_path, class: "btn btn-light", style: "margin-right: 80px;" %> </li> <!-- ログインしていたら --> <% else %> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <figure class="avatar <%= current_user.status ? "online" : "offline" %>"></figure> <%= image_tag avatar_url(current_user), class: "bd-placeholder-img figure-img img-fluid rounded-pill", style: "width: 40px; height: 30px;" %> <%= current_user.full_name %> </a> <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <span class="dropdown-item"><i class="fas fa-user-edit"></i><%= link_to "ユーザ登録情報編集", edit_user_registration_path, class: "btn btn-white" %></span> <hr/> <span class="dropdown-item"><i class="fas fa-sign-out-alt"></i><%= link_to "ログアウト", destroy_user_session_path, method: :delete, class: "btn btn-white" %></span> </div> </li> <% end %> </ul> </div> </nav> <% if (user_signed_in?) %> <nav class="navbar navbar-expand-lg navbar-light bg-light" style="z-index: 4;"> <div class="collapse navbar-collapse justify-content-end" id="navbarNavDropdown"> <ul class="navbar-nav mr-auto"> <li class="nav-item" style="margin-left: 100px; margin-bottom: 5px; margin-right: 80px;"> <span style="margin-top:13px;"><i class="fas fa-tachometer-alt"></i></span><%= link_to 'ダッシュボード', dashboard_path, class: "btn btn-light" %> <span style="margin-top:13px;"><i class="far fa-comment-dots fa-flip-horizontal "></i></i></span><%= link_to 'メッセージの確認', conversations_path, class: "btn btn-light", data: { turbolinks: false} %> </li> <li class="nav-item dropdown" style="margin-right: 50px;"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="fas fa-hospital-symbol"></i> ホスト </a> <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <span class="dropdown-item"><i class="fas fa-edit"></i></i><%= link_to "お部屋を新規登録", new_room_path, class: "btn btn-white" %></span> <span class="dropdown-item"><i class="far fa-list-alt"></i><%= link_to "登録したお部屋一覧", rooms_path, class: "btn btn-white" %></span> <span class="dropdown-item"><i class="far fa-list-alt"></i><%= link_to "受注予約の管理", your_reservations_path, class: "btn btn-white" %></span> <span class="dropdown-item"><i class="far fa-credit-card"></i></i><%= link_to "振込口座の登録", settings_payout_path, class: "btn btn-white" %></span> </div> </li> <li class="nav-item dropdown"> <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="fas fa-user-friends"></i> ゲスト </a> <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink"> <span class="dropdown-item"><i class="far fa-list-alt"></i><%= link_to "ご予約の確認", your_trips_path, class: "btn btn-white" %></span> <span class="dropdown-item"><i class="far fa-credit-card"></i><%= link_to 'クレジットカード登録', settings_payment_path, class: "btn btn-white", data: { turbolinks: false} %></span> </div> </li> </ul> </div> </nav> <% end %>
「app\views」フォルダに「conversations」フォルダを新規作成して下さい。
作成した「conversations」フォルダに「list.html.erb」ファイルを新規作成します。
app\views\conversations\list.html.erb(新規作成したファイル)
<div class="container"> <p class="menu-label"> 全ての会話 </p> <div class="row"> <div class="col-sm-4"> <ul class="menu-list"> <% @conversations.each do |c| %> <% @client = c.sender_id == current_user.id ? c.receiver : c.sender %> <%= link_to conversation_detail_path(@client.id) do %> <li> <div class="card" style="width: 18rem;"> <div style="padding-left: 25px; padding-top: 15px;"><%= image_tag avatar_url(@client), class: "rounded-pill", style: "width: 5rem;" %></div> <div class="card-body"> <p class="card-text"> <%= @client.full_name %><br/> <%= (c.last_message.user_id == current_user.id ? "あなた: #{c.last_message.content}" : c.last_message.content).truncate(20) %><br/> <%= I18n.l(c.last_message.updated_at, format: :full_date) %> </p> </div> </div> </li> <% end %> <% end %> </ul> </div> <div class="col-sm-8"> <p class="subtitle has-text-centered"> 会話を選択して下さい </p> </div> </div> </div>
「app\views\conversations」フォルダに「show.html.erb」ファイル新規作成してください。
app\views\conversations\show.html.erb(新規作成したファイル)
<section class="container" data-channel-subscribe="conversation" data-conversation-id="<%= @conversation.id %>" data-user-id="<%= current_user.id %>"> <div class="row"> <div class="col-md-4"> <nav class="nav"> <p class="menu-label"> 全ての会話 </p> <ul class="menu-list"> <% @conversations.each do |c| %> <% @client = c.sender_id == current_user.id ? c.receiver : c.sender %> <%= link_to conversation_detail_path(@client.id) do %> <li> <div class="card" style="width: 18rem;"> <div style="padding-left: 25px; padding-top: 15px;"><%= image_tag avatar_url(@client), class: "rounded-pill", style: "width: 5rem;" %></div> <div class="card-body"> <p class="card-text"> <%= @client.full_name %><br/> <%= (c.last_message.user_id == current_user.id ? "あなた: #{c.last_message.content}" : c.last_message.content).truncate(20) %><br/> <%= I18n.l(c.last_message.updated_at, format: :full_date) %> </p> </div> </div> </li> <% end %> <% end %> </ul> </nav> </div> <div class="col-md-8"> <!-- メッセージを受けた人 --> <div style="padding-left: 25px; padding-top: 15px;"><%= image_tag avatar_url(@user), class: "rounded-pill", style: "width: 5rem;" %></div> <h3><%= @user.full_name %></h3> <hr> <!-- メッセージ --> <div class="columns is-multiline" id="message_list"> <% @messages.each do |m| %> <%= render 'conversations/message', m: m, user: current_user %> <% end %> </div> <br/> <hr/> <br/> <!-- メッセージを送る --> <div class="column m-t-25"> <%= form_with model: Message.new, id: "new_message" do |f| %> <%= f.hidden_field :receiver_id, value: @user.id %> <%= f.text_field :content, class: "form-control form-control-lg", placeholder: "メッセージを入力してください" %> <br/> <%= f.submit "メッセージを送る", class: "btn btn-primary" %> <% end %> </div> </div> </div> </section>
「app\views\conversations」フォルダに「_message.html.erb」ファイル新規作成してください。
app\views\conversations\_message.html.erb(新規作成したファイル)
<% if m.user.id == user.id %> <div class="col-md-7"> <div style="margin-left: 100px;"> <br/> <%= m.content %><br/> <%= time_ago_in_words(m.created_at) %> </div> </div> <br/> <br/> <% else %> <div class="col-md-7"> <div style="padding-left: 25px; padding-top: 15px;"><%= image_tag avatar_url(m.user), class: "rounded-pill", style: "width: 5rem;" %></div> <%= m.content %><br/> <%= time_ago_in_words(m.created_at) %> </div> <br/> <% end %>
ブラウザ確認
http://localhost:3000/conversations
会話形式でメッセージを送ることができます。
↓↓クリックして頂けると励みになります。
[92]Bootstrap | メッセージと会話 | コントローラとビュー作成<< [ホームに戻る] >> [94]Bootstrap | 通知