質問編集履歴

2

コードの追加

2020/01/03 14:56

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -105,3 +105,141 @@
105
105
  end
106
106
 
107
107
  ```
108
+
109
+ ###index.html.erb
110
+
111
+ ```ここに言語を入力
112
+
113
+ <% @chat_rooms.each do |e| %>
114
+
115
+ <%= e.id %>
116
+
117
+ <% end %>
118
+
119
+ ```
120
+
121
+
122
+
123
+ ###show.html.erb
124
+
125
+ ```ここに言語を入力
126
+
127
+ <% provide(:title, @chat_room_user.username ) %>
128
+
129
+ <div class="container center">
130
+
131
+ <div class="sidebar">
132
+
133
+ <%= link_to root_path(), method: :get do%>
134
+
135
+ <div class="home-btn">
136
+
137
+ <%= image_tag 'right-nav/home', class: 'home-img' %>
138
+
139
+ <span class="home-p">ホーム</span>
140
+
141
+ </div>
142
+
143
+ <% end %>
144
+
145
+ <%= link_to home_path(), method: :get do%>
146
+
147
+ <div class="global-btn">
148
+
149
+ <%= image_tag 'right-nav/global', class: 'global-img'%>
150
+
151
+ <span class="global-p">グローバル</span>
152
+
153
+ </div>
154
+
155
+ <% end %>
156
+
157
+ <% if current_user.try(:admin?) %>
158
+
159
+ <%= link_to rails_admin_path(), method: :get do%>
160
+
161
+ <div class="control-btn">
162
+
163
+ <%= image_tag 'right-nav/control.svg', class: 'control-img'%>
164
+
165
+ <span class="control-p">管理画面</span>
166
+
167
+ </div>
168
+
169
+ <% end %>
170
+
171
+ <% end %>
172
+
173
+ <div class="group-btn">
174
+
175
+ <%= image_tag 'right-nav/group', class: 'group-img'%>
176
+
177
+ <span class="group-p">グループ</span>
178
+
179
+ </div>
180
+
181
+ </div>
182
+
183
+ <div class="main">
184
+
185
+ <% if @chat_room_myuser.id == @chat_room_user.id %>
186
+
187
+ <header class="header">
188
+
189
+ <div class="chatPartner">
190
+
191
+ <img src="<%= @chat_room_user.image %>" class="dm-icon">
192
+
193
+ <div class="dm-username"><%= @chat_room_user.username %></div>
194
+
195
+ <div class="dm-userid">@<%= @chat_room_user.user_id %></div>
196
+
197
+ </div>
198
+
199
+ </header>
200
+
201
+ <div class="chat-history">
202
+
203
+ <%= render @chat_messages %>
204
+
205
+ <% @chat_rooms.each do |e| %>
206
+
207
+ <%= e.id %>
208
+
209
+ <% end %>
210
+
211
+ </div>
212
+
213
+ <div class="text-box">
214
+
215
+ <form class="messageInputForm">
216
+
217
+ <div class="container">
218
+
219
+ <input type="text" data-behavior="room_speaker" class="messageInputForm_input" placeholder="メッセージを入力...">
220
+
221
+ </div>
222
+
223
+ </form>
224
+
225
+ </div>
226
+
227
+ </div>
228
+
229
+ </div>
230
+
231
+
232
+
233
+ <% else %>
234
+
235
+ <div class="error-message">
236
+
237
+ <p class="sorry-chat">Sorry</p>
238
+
239
+ <p class="sorry-text">ページを表示する権限がありません</p>
240
+
241
+ </div>
242
+
243
+ <% end %>
244
+
245
+ ```

1

コードの編集

2020/01/03 14:56

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,16 +8,100 @@
8
8
 
9
9
 
10
10
 
11
+ ###chat_controller.rb
12
+
11
13
  ```ここに言語を入力
14
+
15
+ class ChatController < ApplicationController
16
+
17
+ def create
18
+
19
+ current_user_chat_rooms = ChatRoomUser.where(user_id: current_user.id).map(&:chat_room)
20
+
21
+ chat_room = ChatRoomUser.where(chat_room: current_user_chat_rooms, user_id: params[:user_id]).map(&:chat_room).first
22
+
23
+ if chat_room.blank?
24
+
25
+ chat_room = ChatRoom.create
26
+
27
+ ChatRoomUser.create(chat_room: chat_room, user_id: current_user.id)
28
+
29
+ ChatRoomUser.create(chat_room: chat_room, user_id: params[:user_id])
30
+
31
+ end
32
+
33
+ redirect_to action: :show, id: chat_room.id
34
+
35
+ end
12
36
 
13
37
  def index
14
38
 
15
- @current_user = User.find_by(id: session[:user_id])
39
+ @current_user = User.find_by(user_id: params[:user_id])
16
40
 
17
41
  @chat_rooms = ChatRoom.joins(:chat_room_users).
18
42
 
19
- where("user.id =?", @current_user.id)
43
+ where("chat_room_users.user_id =?", @current_user.id)
20
44
 
21
45
  end
22
46
 
47
+
48
+
49
+
50
+
51
+ def show
52
+
53
+ chat_room = ChatRoom.find_by(id: params[:id])
54
+
55
+ @current_user = User.find_by(id: session[:user_id])
56
+
57
+ @chat_room_user = chat_room.chat_room_users.
58
+
59
+ where.not(user_id: current_user.id).first.user
60
+
61
+ @chat_room_myuser = chat_room.chat_room_users.
62
+
63
+ where.not(user_id: current_user.id).last.user
64
+
65
+ @chat_rooms = ChatRoom.joins(:chat_room_users).
66
+
67
+ where("chat_room_users.user_id =?", @current_user.id)
68
+
69
+ @chat_messages = ChatMessage.where(chat_room: chat_room).order(:created_at)
70
+
71
+ end
72
+
73
+ end
74
+
23
75
  ```
76
+
77
+
78
+
79
+ ###chat_room.rb
80
+
81
+ ```ここに言語を入力
82
+
83
+ class ChatRoom < ApplicationRecord
84
+
85
+ has_many :chat_messages
86
+
87
+ has_many :chat_room_users
88
+
89
+ end
90
+
91
+
92
+
93
+ ```
94
+
95
+ ###chat_room_user.rb
96
+
97
+ ```ここに言語を入力
98
+
99
+ class ChatRoomUser < ApplicationRecord
100
+
101
+ belongs_to :chat_room
102
+
103
+ belongs_to :user
104
+
105
+ end
106
+
107
+ ```