質問編集履歴
4
コードの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -200,6 +200,16 @@
|
|
200
200
|
|
201
201
|
end
|
202
202
|
|
203
|
+
|
204
|
+
|
205
|
+
private
|
206
|
+
|
207
|
+
def message_params
|
208
|
+
|
209
|
+
params.require(:message).permit(:body, :user_id)
|
210
|
+
|
211
|
+
end
|
212
|
+
|
203
213
|
```
|
204
214
|
|
205
215
|
|
3
コードの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -40,8 +40,24 @@
|
|
40
40
|
|
41
41
|
###該当のソースコード
|
42
42
|
|
43
|
+
問題があると思われるviewのコード。
|
44
|
+
|
45
|
+
また、今回メッセージ機能は会話「conversations」、メッセージ「messages」として関連づけて作成したので、そちらのコントローラーとモデルなども記載します。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
``views/messages/index.html.erb``
|
50
|
+
|
43
51
|
```views/messages/index.html.erb
|
44
52
|
|
53
|
+
<% if @over_ten %>
|
54
|
+
|
55
|
+
<%= link_to '以前のメッセージをすべて表示', '?m=all' %>
|
56
|
+
|
57
|
+
<% end %>
|
58
|
+
|
59
|
+
|
60
|
+
|
45
61
|
<div class="ui segment">
|
46
62
|
|
47
63
|
<% @messages.each do |message| %>
|
@@ -86,6 +102,270 @@
|
|
86
102
|
|
87
103
|
</div>
|
88
104
|
|
105
|
+
|
106
|
+
|
107
|
+
<!-- メッセージの送信 -->
|
108
|
+
|
109
|
+
<%= form_for [@conversation, @message], html: {class: "ui reply form"} do |f| %>
|
110
|
+
|
111
|
+
<div class="field">
|
112
|
+
|
113
|
+
<%= f.text_area :body, class: "form-control" %>
|
114
|
+
|
115
|
+
</div>
|
116
|
+
|
117
|
+
<%= f.text_field :user_id, value: current_user.id, type: "hidden" %>
|
118
|
+
|
119
|
+
<div>
|
120
|
+
|
121
|
+
<%= f.submit "メッセージを送る" %>
|
122
|
+
|
123
|
+
</div>
|
124
|
+
|
125
|
+
<% end %>
|
126
|
+
|
127
|
+
```
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
``messages_controller.rb``
|
132
|
+
|
133
|
+
```messages_controller.rb
|
134
|
+
|
135
|
+
class MessagesController < ApplicationController
|
136
|
+
|
137
|
+
before_action :authenticate_user!
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
before_action do
|
142
|
+
|
143
|
+
@conversation = Conversation.find(params[:conversation_id])
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
def index
|
150
|
+
|
151
|
+
@messages = @conversation.messages
|
152
|
+
|
153
|
+
if @messages.length > 10
|
154
|
+
|
155
|
+
@over_ten = true
|
156
|
+
|
157
|
+
@messages = @messages[-10..-1]
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
if params[:m]
|
164
|
+
|
165
|
+
@over_ten = false
|
166
|
+
|
167
|
+
@messages = @conversation.messages
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
if @messages.last
|
174
|
+
|
175
|
+
if @messages.last.user_id != current_user.id
|
176
|
+
|
177
|
+
@messages.last.read = true
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
@message = @conversation.messages.build
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
def create
|
192
|
+
|
193
|
+
@message = @conversation.messages.build(message_params)
|
194
|
+
|
195
|
+
if @message.save
|
196
|
+
|
197
|
+
redirect_to conversation_messages_path(@conversation)
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
```
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
``messageモデル``
|
208
|
+
|
209
|
+
```
|
210
|
+
|
211
|
+
class Message < ActiveRecord::Base
|
212
|
+
|
213
|
+
belongs_to :conversation
|
214
|
+
|
215
|
+
belongs_to :user
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
validates_presence_of :body, :conversation_id, :user_id
|
220
|
+
|
221
|
+
def message_time
|
222
|
+
|
223
|
+
created_at.strftime("%m/%d/%y at %l:%M %p")
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
```
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
``conversations_controller.rb``
|
234
|
+
|
235
|
+
```
|
236
|
+
|
237
|
+
class ConversationsController < ApplicationController
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
before_action :authenticate_user!
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
def index
|
246
|
+
|
247
|
+
@users = User.all
|
248
|
+
|
249
|
+
@conversations = Conversation.all
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
def create
|
256
|
+
|
257
|
+
if Conversation.between(params[:sender_id], params[:recipient_id]).present?
|
258
|
+
|
259
|
+
@conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first
|
260
|
+
|
261
|
+
else
|
262
|
+
|
263
|
+
@conversation = Conversation.create!(conversation_params)
|
264
|
+
|
265
|
+
end
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
redirect_to conversation_messages_path(@conversation)
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
private
|
276
|
+
|
277
|
+
def conversation_params
|
278
|
+
|
279
|
+
params.permit(:sender_id, :recipient_id)
|
280
|
+
|
281
|
+
end
|
282
|
+
|
283
|
+
end
|
284
|
+
|
285
|
+
```
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
``Conversationモデル``
|
290
|
+
|
291
|
+
```
|
292
|
+
|
293
|
+
class Conversation < ActiveRecord::Base
|
294
|
+
|
295
|
+
belongs_to :sender, foreign_key: :sender_id, class_name: 'User'
|
296
|
+
|
297
|
+
belongs_to :recipient, foreign_key: :recipient_id, class_name: 'User'
|
298
|
+
|
299
|
+
has_many :messages, dependent: :destroy
|
300
|
+
|
301
|
+
validates_uniqueness_of :sender_id, scope: :recipient_id
|
302
|
+
|
303
|
+
scope :between, -> (sender_id,recipient_id) do
|
304
|
+
|
305
|
+
where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
|
306
|
+
|
307
|
+
end
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
def target_user(current_user)
|
312
|
+
|
313
|
+
if sender_id == current_user.id
|
314
|
+
|
315
|
+
User.find(recipient_id)
|
316
|
+
|
317
|
+
elsif recipient_id == current_user.id
|
318
|
+
|
319
|
+
User.find(sender_id)
|
320
|
+
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
end
|
326
|
+
|
327
|
+
```
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
``データベーステーブル``
|
332
|
+
|
333
|
+
```
|
334
|
+
|
335
|
+
create_table "messages", force: :cascade do |t|
|
336
|
+
|
337
|
+
t.text "body"
|
338
|
+
|
339
|
+
t.integer "conversation_id"
|
340
|
+
|
341
|
+
t.integer "user_id"
|
342
|
+
|
343
|
+
t.boolean "read", default: false
|
344
|
+
|
345
|
+
t.datetime "created_at", null: false
|
346
|
+
|
347
|
+
t.datetime "updated_at", null: false
|
348
|
+
|
349
|
+
end
|
350
|
+
|
351
|
+
add_index "messages", ["conversation_id"], name: "index_messages_on_conversation_id", using: :btree
|
352
|
+
|
353
|
+
add_index "messages", ["user_id"], name: "index_messages_on_user_id", using: :btree
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
create_table "conversations", force: :cascade do |t|
|
358
|
+
|
359
|
+
t.integer "sender_id"
|
360
|
+
|
361
|
+
t.integer "recipient_id"
|
362
|
+
|
363
|
+
t.datetime "created_at", null: false
|
364
|
+
|
365
|
+
t.datetime "updated_at", null: false
|
366
|
+
|
367
|
+
end
|
368
|
+
|
89
369
|
```
|
90
370
|
|
91
371
|
|
2
エラー内容の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -17,6 +17,22 @@
|
|
17
17
|
|
18
18
|
|
19
19
|
最初に試してみたときは正常に表示できたのですが、機能テストのためユーザーを増やして試してみたところ、先ほどの``Couldn't find User with 'id'=``というエラーになってしまいました。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
あと、
|
24
|
+
|
25
|
+
```
|
26
|
+
|
27
|
+
<% if @over_ten %>
|
28
|
+
|
29
|
+
<%= link_to '以前のメッセージをすべて表示', '?m=all' %>
|
30
|
+
|
31
|
+
<% end %>
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
とこのように10件以上の表示はリンクをクリックして表示させているのですが、正常に表示さえているユーザーとのメッセージもこのリンクをクリックすると``Couldn't find User with 'id'=``と同じようにエラーになってしまいます。
|
20
36
|
|
21
37
|
|
22
38
|
|
1
初心者マークを追加
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|