質問編集履歴
10
ログの修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -149,20 +149,21 @@
|
|
149
149
|
```Rails
|
150
150
|
【ログ】
|
151
151
|
|
152
|
-
Started POST "/rooms" for ::1 at 2020-10-30
|
152
|
+
Started POST "/rooms" for ::1 at 2020-10-30 21:19:38 +0900
|
153
153
|
Processing by RoomsController#create as HTML
|
154
|
-
Parameters: {"authenticity_token"=>"
|
154
|
+
Parameters: {"authenticity_token"=>"29rqX0N8xw8O14Yoppch1Eu4jo+rGqHvvwQWeqjWLCbFCeHyK9RMsLsywC8WiRaZbQNh8cOAcOLY1UAuczVQ4Q==", "room"=>{"name"=>"test", "user_ids"=>["2", "1"]}, "commit"=>"登録する"}
|
155
|
-
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` =
|
155
|
+
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
|
156
156
|
Unpermitted parameter: :user_ids
|
157
|
-
(0.
|
157
|
+
(0.2ms) BEGIN
|
158
158
|
↳ app/controllers/rooms_controller.rb:15:in `create'
|
159
|
-
Room Create (0.
|
159
|
+
Room Create (0.3ms) INSERT INTO `rooms` (`name`, `created_at`, `updated_at`) VALUES ('test', '2020-10-30 12:19:38.500111', '2020-10-30 12:19:38.500111')
|
160
160
|
↳ app/controllers/rooms_controller.rb:15:in `create'
|
161
|
-
(
|
161
|
+
(1.9ms) COMMIT
|
162
162
|
↳ app/controllers/rooms_controller.rb:15:in `create'
|
163
163
|
Redirected to http://localhost:3000/
|
164
|
-
Completed 302 Found in
|
164
|
+
Completed 302 Found in 10ms (ActiveRecord: 2.8ms | Allocations: 5200)
|
165
165
|
|
166
|
+
|
166
167
|
```
|
167
168
|
```
|
168
169
|
更新日時:10月30日(金) 14:20
|
9
修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
中間テーブルに値が入らない (バリデーションに失敗しました: Usersは不正な値です)
|
body
CHANGED
@@ -10,10 +10,8 @@
|
|
10
10
|
値が入っていない。
|
11
11
|
```
|
12
12
|
|
13
|
-
|
14
|
-
|
15
13
|
### 該当のソースコード
|
16
|
-
|
14
|
+
```
|
17
15
|
```Rails
|
18
16
|
【new.html.erb】
|
19
17
|
class RoomsController < ApplicationController
|
8
修正
title
CHANGED
File without changes
|
body
CHANGED
@@ -166,4 +166,5 @@
|
|
166
166
|
Completed 302 Found in 8ms (ActiveRecord: 1.3ms | Allocations: 5201)
|
167
167
|
|
168
168
|
```
|
169
|
+
```
|
169
170
|
更新日時:10月30日(金) 14:20
|
7
更新
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,28 +5,17 @@
|
|
5
5
|
|
6
6
|
|
7
7
|
### 発生している問題・エラーメッセージ
|
8
|
-
|
8
|
+
roomを作成する際に、roomに入れるuserも選択肢、中間テーブルにroom_idとuser_idを保存したい。
|
9
|
-
しかし、
|
9
|
+
しかし、room作成ボタンを押した後、roomテーブルにのみ値が入っており、room_usersテーブル(中間テーブル)には
|
10
|
-
|
11
|
-
parameterを見たところ、userのid(現在のユーザーと、選択した他のユーザーのid)は値として入っていますが、room_idがnilになっていることが問題なのかもしれません。
|
12
|
-
|
13
|
-
もし、そこが問題ならどのようにroom_idを中間テーブルに入れることができますか?
|
14
|
-
|
15
|
-
|
10
|
+
値が入っていない。
|
16
|
-
|
17
11
|
```
|
18
|
-
ActiveRecord::RecordInvalid in RoomsController#create
|
19
|
-
バリデーションに失敗しました: Usersは不正な値です
|
20
12
|
|
21
|
-
```
|
22
13
|
|
23
14
|
|
24
|
-
|
25
15
|
### 該当のソースコード
|
26
16
|
|
27
17
|
```Rails
|
28
|
-
【
|
18
|
+
【new.html.erb】
|
29
|
-
|
30
19
|
class RoomsController < ApplicationController
|
31
20
|
|
32
21
|
|
@@ -36,12 +25,13 @@
|
|
36
25
|
|
37
26
|
def new
|
38
27
|
@room = Room.new
|
28
|
+
@room.room_users.build
|
39
29
|
end
|
40
30
|
|
41
31
|
def create
|
42
32
|
@room = Room.new(room_params)
|
43
33
|
if @room.save
|
44
|
-
redirect_to root_path
|
34
|
+
redirect_to root_path
|
45
35
|
else
|
46
36
|
redirect_to new_room_path
|
47
37
|
end
|
@@ -56,159 +46,14 @@
|
|
56
46
|
private
|
57
47
|
|
58
48
|
def room_params
|
59
|
-
params.require(:room).permit(:name, user_ids:
|
49
|
+
params.require(:room).permit(:name, room_users_attributes:{user_ids:[]})
|
60
50
|
end
|
61
51
|
|
62
52
|
end
|
63
|
-
```
|
64
53
|
|
65
|
-
```Rails
|
66
|
-
【room_usersテーブルのマイグレーションファイル】
|
67
|
-
|
68
|
-
class CreateRoomUsers < ActiveRecord::Migration[6.0]
|
69
|
-
def change
|
70
|
-
create_table :room_users do |t|
|
71
|
-
t.references :room, foreign_key: true
|
72
|
-
t.references :user, foreign_key: true
|
73
|
-
t.timestamps
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
54
|
```
|
79
55
|
|
80
56
|
```Rails
|
81
|
-
【users_controller.rb】
|
82
|
-
|
83
|
-
class UsersController < ApplicationController
|
84
|
-
def index
|
85
|
-
end
|
86
|
-
|
87
|
-
def new
|
88
|
-
@user = User.new
|
89
|
-
end
|
90
|
-
|
91
|
-
def create
|
92
|
-
@user = User.new(user_params)
|
93
|
-
if current_user.save
|
94
|
-
redirect_to root_path
|
95
|
-
else
|
96
|
-
render :new
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
def edit
|
101
|
-
end
|
102
|
-
|
103
|
-
def update
|
104
|
-
if current_user.update(user_params)
|
105
|
-
redirect_to root_path
|
106
|
-
else
|
107
|
-
render :edit
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
private
|
112
|
-
|
113
|
-
def user_params
|
114
|
-
params.require(:user).permit(
|
115
|
-
:name,
|
116
|
-
:email,
|
117
|
-
:password,
|
118
|
-
:password_confirm,
|
119
|
-
:genre_id,
|
120
|
-
:grade_id,
|
121
|
-
:number_id,
|
122
|
-
:class_number_id
|
123
|
-
)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
```
|
128
|
-
|
129
|
-
```Rails
|
130
|
-
【room.rb】
|
131
|
-
|
132
|
-
class Room < ApplicationRecord
|
133
|
-
|
134
|
-
has_many :room_users
|
135
|
-
has_many :users, through: :room_users
|
136
|
-
has_many :messages, dependent: :destroy
|
137
|
-
|
138
|
-
validates :name, presence: true
|
139
|
-
end
|
140
|
-
|
141
|
-
```
|
142
|
-
|
143
|
-
```Rails
|
144
|
-
【user.rb】
|
145
|
-
|
146
|
-
class User < ApplicationRecord
|
147
|
-
|
148
|
-
extend ActiveHash::Associations::ActiveRecordExtensions
|
149
|
-
|
150
|
-
devise :database_authenticatable, :registerable,
|
151
|
-
:recoverable, :rememberable, :validatable
|
152
|
-
|
153
|
-
belongs_to_active_hash :genre
|
154
|
-
belongs_to_active_hash :grade
|
155
|
-
belongs_to_active_hash :class_number
|
156
|
-
belongs_to_active_hash :number
|
157
|
-
|
158
|
-
has_many :room_users
|
159
|
-
has_many :rooms, through: :room_users
|
160
|
-
has_many :messages
|
161
|
-
|
162
|
-
with_options presence: true do
|
163
|
-
validates :name
|
164
|
-
validates :email
|
165
|
-
validates :password
|
166
|
-
|
167
|
-
with_options numericality: { other_than: 1} do
|
168
|
-
validates :genre_id
|
169
|
-
validates :grade_id
|
170
|
-
validates :class_number_id
|
171
|
-
validates :number_id
|
172
|
-
end
|
173
|
-
end
|
174
|
-
end
|
175
|
-
|
176
|
-
```
|
177
|
-
|
178
|
-
```Rails
|
179
|
-
【room_users.rb】
|
180
|
-
|
181
|
-
class RoomUser < ApplicationRecord
|
182
|
-
belongs_to :room, optional: true
|
183
|
-
belongs_to :user, optional: true
|
184
|
-
end
|
185
|
-
|
186
|
-
```
|
187
|
-
|
188
|
-
|
189
|
-
### 試したこと
|
190
|
-
サイトで調べた結果、中間テーブルにある、user_idとroom_idがbigintになっているのを、integerに変更したが、特別変化は起きていない。(現在はまたbigintに戻している)
|
191
|
-
|
192
|
-
|
193
|
-
### 補足情報(FW/ツールのバージョンなど)
|
194
|
-
【binding.pry後、@roomの値を確認した結果】
|
195
|
-
|
196
|
-
12: def create
|
197
|
-
13: @room = Room.new(room_params)
|
198
|
-
=> 14: binding.pry
|
199
|
-
15: if @room.save!
|
200
|
-
16: redirect_to root_path
|
201
|
-
17: else
|
202
|
-
18: redirect_to new_room_path
|
203
|
-
19: end
|
204
|
-
20: end
|
205
|
-
|
206
|
-
[1] pry(#<RoomsController>)> @room
|
207
|
-
=> #<Room:0x00007f855aaac5a8 id: nil, name: "新規作成", created_at: nil, updated_at: nil>
|
208
|
-
|
209
|
-
```Rails
|
210
|
-
【(room)new.html.erb】
|
211
|
-
|
212
57
|
<div class='chat-room-form'>
|
213
58
|
<h1>新規チャットルーム</h1>
|
214
59
|
<%=form_with model: @room, local: true do |f|%>
|
@@ -247,84 +92,52 @@
|
|
247
92
|
```
|
248
93
|
|
249
94
|
```Rails
|
250
|
-
|
95
|
+
【room_users.rb(中間テーブル)】
|
251
96
|
|
97
|
+
class RoomUser < ApplicationRecord
|
98
|
+
belongs_to :room, optional: true
|
99
|
+
belongs_to :user, optional: true
|
100
|
+
end
|
101
|
+
```
|
252
102
|
|
103
|
+
```Rails
|
253
|
-
|
104
|
+
【user.rb】
|
254
105
|
|
255
|
-
|
106
|
+
class User < ApplicationRecord
|
256
107
|
|
257
|
-
def new
|
258
|
-
@room = Room.new
|
259
|
-
|
108
|
+
extend ActiveHash::Associations::ActiveRecordExtensions
|
260
|
-
end
|
261
109
|
|
262
|
-
|
110
|
+
devise :database_authenticatable, :registerable,
|
263
|
-
|
111
|
+
:recoverable, :rememberable, :validatable
|
264
|
-
|
112
|
+
|
265
|
-
# binding.pry
|
266
|
-
|
113
|
+
belongs_to_active_hash :genre
|
267
|
-
else
|
268
|
-
|
114
|
+
belongs_to_active_hash :grade
|
115
|
+
belongs_to_active_hash :class_number
|
269
|
-
|
116
|
+
belongs_to_active_hash :number
|
270
|
-
end
|
271
117
|
|
272
|
-
def destroy
|
273
|
-
room = Room.find(params[:id])
|
274
|
-
room.destroy
|
275
|
-
|
118
|
+
has_many :room_users
|
119
|
+
has_many :rooms, through: :room_users
|
120
|
+
has_many :messages
|
121
|
+
accepts_nested_attributes_for :room_users
|
122
|
+
|
123
|
+
with_options presence: true do
|
276
|
-
|
124
|
+
validates :name
|
125
|
+
validates :email
|
126
|
+
validates :password
|
277
127
|
|
128
|
+
with_options numericality: { other_than: 1} do
|
278
|
-
|
129
|
+
validates :genre_id
|
279
|
-
|
130
|
+
validates :grade_id
|
131
|
+
validates :class_number_id
|
280
|
-
|
132
|
+
validates :number_id
|
281
|
-
params.require(:room).permit(:name, user_ids_attributes: [])
|
282
|
-
|
133
|
+
end
|
283
|
-
|
134
|
+
end
|
284
135
|
end
|
285
136
|
```
|
286
137
|
|
287
|
-
|
288
|
-
10月28日(水) 17:22 更新
|
289
138
|
```Rails
|
290
|
-
<div class='chat-room-form'>
|
291
|
-
<h1>新規チャットルーム</h1>
|
292
|
-
<%=form_with model: @room, local: true do |f|%>
|
293
|
-
<div class='chat-room-form__field'>
|
294
|
-
<div class='chat-room-form__field--left'>
|
295
|
-
<%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
|
296
|
-
</div>
|
297
|
-
<div class='chat-room-form__field--right'>
|
298
|
-
<%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
|
299
|
-
</div>
|
300
|
-
</div>
|
301
|
-
<div class='chat-room-form__field'>
|
302
|
-
</div>
|
303
|
-
<div class='chat-room-form__field'>
|
304
|
-
<div class='chat-room-form__field--left'>
|
305
|
-
<label class='chat-room-form__label' for='chat_room_チャットメンバー'>チャットメンバー</label>
|
306
|
-
</div>
|
307
|
-
<div class='chat-room-form__field--right'>
|
308
|
-
<select name="room[user_ids][]">
|
309
|
-
<option value="">チャットするユーザーを選択してください</option>
|
310
|
-
<% User.where.not(id: current_user.id).each do |user| %>
|
311
|
-
<option value=<%= user.id %>><%= user.name %></option>
|
312
|
-
|
139
|
+
【room.rb】
|
313
|
-
</select>
|
314
|
-
<%# <input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>> %>
|
315
|
-
</div>
|
316
|
-
</div>
|
317
|
-
<div class='chat-room-form__field'>
|
318
|
-
<div class='chat-room-form__field--left'></div>
|
319
|
-
<div class='chat-room-form__field--right'>
|
320
|
-
<%= f.submit class: 'chat-room-form__action-btn'%>
|
321
|
-
</div>
|
322
|
-
</div>
|
323
|
-
<% end %>
|
324
|
-
</div>
|
325
|
-
```
|
326
140
|
|
327
|
-
```Rails
|
328
141
|
class Room < ApplicationRecord
|
329
142
|
|
330
143
|
has_many :room_users
|
@@ -335,22 +148,22 @@
|
|
335
148
|
end
|
336
149
|
```
|
337
150
|
|
338
|
-
|
151
|
+
```Rails
|
152
|
+
【ログ】
|
339
153
|
|
340
|
-
room作成ボタンを押した際の、現在のログの状況
|
341
|
-
```Rails
|
342
|
-
Started POST "/rooms" for ::1 at 2020-10-
|
154
|
+
Started POST "/rooms" for ::1 at 2020-10-30 14:18:41 +0900
|
343
155
|
Processing by RoomsController#create as HTML
|
344
|
-
Parameters: {"authenticity_token"=>"
|
156
|
+
Parameters: {"authenticity_token"=>"QboJSgKi561sC1fc0YvaFeYoqnalb1F2r/lfHSmYD8hLa5FM6DWquLiZTHxExjr+0ZsGp/X2mo7nbl9uSiQ8Sg==", "room"=>{"name"=>"テスト", "user_ids"=>["1", "2"]}, "commit"=>"登録する"}
|
345
|
-
User Load (0.
|
157
|
+
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
|
346
158
|
Unpermitted parameter: :user_ids
|
347
159
|
(0.1ms) BEGIN
|
348
160
|
↳ app/controllers/rooms_controller.rb:15:in `create'
|
349
|
-
Room Create (0.
|
161
|
+
Room Create (0.2ms) INSERT INTO `rooms` (`name`, `created_at`, `updated_at`) VALUES ('テスト', '2020-10-30 05:18:41.825926', '2020-10-30 05:18:41.825926')
|
350
162
|
↳ app/controllers/rooms_controller.rb:15:in `create'
|
351
|
-
(0.
|
163
|
+
(0.5ms) COMMIT
|
352
164
|
↳ app/controllers/rooms_controller.rb:15:in `create'
|
353
165
|
Redirected to http://localhost:3000/
|
354
|
-
Completed 302 Found in 8ms (ActiveRecord: 1.
|
166
|
+
Completed 302 Found in 8ms (ActiveRecord: 1.3ms | Allocations: 5201)
|
355
167
|
|
356
|
-
```
|
168
|
+
```
|
169
|
+
更新日時:10月30日(金) 14:20
|
6
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -335,12 +335,22 @@
|
|
335
335
|
end
|
336
336
|
```
|
337
337
|
|
338
|
+
更新 10月28日(水) 20:58
|
339
|
+
|
338
|
-
room作成ボタンを押した際の、現在の
|
340
|
+
room作成ボタンを押した際の、現在のログの状況
|
339
341
|
```Rails
|
340
|
-
Started
|
342
|
+
Started POST "/rooms" for ::1 at 2020-10-28 20:47:17 +0900
|
341
|
-
Processing by RoomsController#
|
343
|
+
Processing by RoomsController#create as HTML
|
344
|
+
Parameters: {"authenticity_token"=>"HWt4O6MU9KiYhIPCzbdoCK73UT9ID61wWCn3rjOj/M4XuuA9SYO5vUwWmGJY+ojjmUT97hiWZogQvvfdUB/PTA==", "room"=>{"name"=>"新規作成", "user_ids"=>["1", "2"]}, "commit"=>"登録する"}
|
342
|
-
User Load (0.
|
345
|
+
User Load (0.5ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
|
346
|
+
Unpermitted parameter: :user_ids
|
347
|
+
(0.1ms) BEGIN
|
343
|
-
|
348
|
+
↳ app/controllers/rooms_controller.rb:15:in `create'
|
344
|
-
Room
|
349
|
+
Room Create (0.3ms) INSERT INTO `rooms` (`name`, `created_at`, `updated_at`) VALUES ('新規作成', '2020-10-28 11:47:17.906618', '2020-10-28 11:47:17.906618')
|
350
|
+
↳ app/controllers/rooms_controller.rb:15:in `create'
|
351
|
+
(0.3ms) COMMIT
|
352
|
+
↳ app/controllers/rooms_controller.rb:15:in `create'
|
353
|
+
Redirected to http://localhost:3000/
|
354
|
+
Completed 302 Found in 8ms (ActiveRecord: 1.1ms | Allocations: 5182)
|
345
355
|
|
346
356
|
```
|
5
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -285,7 +285,7 @@
|
|
285
285
|
```
|
286
286
|
|
287
287
|
|
288
|
-
10月28日(水) 17:
|
288
|
+
10月28日(水) 17:22 更新
|
289
289
|
```Rails
|
290
290
|
<div class='chat-room-form'>
|
291
291
|
<h1>新規チャットルーム</h1>
|
@@ -311,7 +311,7 @@
|
|
311
311
|
<option value=<%= user.id %>><%= user.name %></option>
|
312
312
|
<% end %>
|
313
313
|
</select>
|
314
|
-
<input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>>
|
314
|
+
<%# <input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>> %>
|
315
315
|
</div>
|
316
316
|
</div>
|
317
317
|
<div class='chat-room-form__field'>
|
@@ -333,4 +333,14 @@
|
|
333
333
|
accepts_nested_attributes_for :room_users
|
334
334
|
validates :name, presence: true
|
335
335
|
end
|
336
|
+
```
|
337
|
+
|
338
|
+
room作成ボタンを押した際の、現在のターミナルの状況
|
339
|
+
```Rails
|
340
|
+
Started GET "/" for ::1 at 2020-10-28 17:19:39 +0900
|
341
|
+
Processing by RoomsController#index as HTML
|
342
|
+
User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
|
343
|
+
Rendering rooms/index.html.erb within layouts/application
|
344
|
+
Room Load (0.3ms) SELECT `rooms`.* FROM `rooms` INNER JOIN `room_users` ON `rooms`.`id` = `room_users`.`room_id` WHERE `room_users`.`user_id` = 2
|
345
|
+
|
336
346
|
```
|
4
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -244,4 +244,93 @@
|
|
244
244
|
</div>
|
245
245
|
<% end %>
|
246
246
|
</div>
|
247
|
+
```
|
248
|
+
|
249
|
+
```Rails
|
250
|
+
class RoomsController < ApplicationController
|
251
|
+
|
252
|
+
|
253
|
+
def index
|
254
|
+
|
255
|
+
end
|
256
|
+
|
257
|
+
def new
|
258
|
+
@room = Room.new
|
259
|
+
@room.room_users.build
|
260
|
+
end
|
261
|
+
|
262
|
+
def create
|
263
|
+
@room = Room.new(room_params)
|
264
|
+
if @room.save
|
265
|
+
# binding.pry
|
266
|
+
redirect_to root_path
|
267
|
+
else
|
268
|
+
redirect_to new_room_path
|
269
|
+
end
|
270
|
+
end
|
271
|
+
|
272
|
+
def destroy
|
273
|
+
room = Room.find(params[:id])
|
274
|
+
room.destroy
|
275
|
+
redirect_to root_path
|
276
|
+
end
|
277
|
+
|
278
|
+
private
|
279
|
+
|
280
|
+
def room_params
|
281
|
+
params.require(:room).permit(:name, user_ids_attributes: [])
|
282
|
+
end
|
283
|
+
|
284
|
+
end
|
285
|
+
```
|
286
|
+
|
287
|
+
|
288
|
+
10月28日(水) 17:18 更新
|
289
|
+
```Rails
|
290
|
+
<div class='chat-room-form'>
|
291
|
+
<h1>新規チャットルーム</h1>
|
292
|
+
<%=form_with model: @room, local: true do |f|%>
|
293
|
+
<div class='chat-room-form__field'>
|
294
|
+
<div class='chat-room-form__field--left'>
|
295
|
+
<%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
|
296
|
+
</div>
|
297
|
+
<div class='chat-room-form__field--right'>
|
298
|
+
<%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
|
299
|
+
</div>
|
300
|
+
</div>
|
301
|
+
<div class='chat-room-form__field'>
|
302
|
+
</div>
|
303
|
+
<div class='chat-room-form__field'>
|
304
|
+
<div class='chat-room-form__field--left'>
|
305
|
+
<label class='chat-room-form__label' for='chat_room_チャットメンバー'>チャットメンバー</label>
|
306
|
+
</div>
|
307
|
+
<div class='chat-room-form__field--right'>
|
308
|
+
<select name="room[user_ids][]">
|
309
|
+
<option value="">チャットするユーザーを選択してください</option>
|
310
|
+
<% User.where.not(id: current_user.id).each do |user| %>
|
311
|
+
<option value=<%= user.id %>><%= user.name %></option>
|
312
|
+
<% end %>
|
313
|
+
</select>
|
314
|
+
<input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>>
|
315
|
+
</div>
|
316
|
+
</div>
|
317
|
+
<div class='chat-room-form__field'>
|
318
|
+
<div class='chat-room-form__field--left'></div>
|
319
|
+
<div class='chat-room-form__field--right'>
|
320
|
+
<%= f.submit class: 'chat-room-form__action-btn'%>
|
321
|
+
</div>
|
322
|
+
</div>
|
323
|
+
<% end %>
|
324
|
+
</div>
|
325
|
+
```
|
326
|
+
|
327
|
+
```Rails
|
328
|
+
class Room < ApplicationRecord
|
329
|
+
|
330
|
+
has_many :room_users
|
331
|
+
has_many :users, through: :room_users
|
332
|
+
has_many :messages, dependent: :destroy
|
333
|
+
accepts_nested_attributes_for :room_users
|
334
|
+
validates :name, presence: true
|
335
|
+
end
|
247
336
|
```
|
3
new.html.erbの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -204,4 +204,44 @@
|
|
204
204
|
20: end
|
205
205
|
|
206
206
|
[1] pry(#<RoomsController>)> @room
|
207
|
-
=> #<Room:0x00007f855aaac5a8 id: nil, name: "新規作成", created_at: nil, updated_at: nil>
|
207
|
+
=> #<Room:0x00007f855aaac5a8 id: nil, name: "新規作成", created_at: nil, updated_at: nil>
|
208
|
+
|
209
|
+
```Rails
|
210
|
+
【(room)new.html.erb】
|
211
|
+
|
212
|
+
<div class='chat-room-form'>
|
213
|
+
<h1>新規チャットルーム</h1>
|
214
|
+
<%=form_with model: @room, local: true do |f|%>
|
215
|
+
<div class='chat-room-form__field'>
|
216
|
+
<div class='chat-room-form__field--left'>
|
217
|
+
<%= f.label :チャットルーム名, class: 'chat-room-form__label'%>
|
218
|
+
</div>
|
219
|
+
<div class='chat-room-form__field--right'>
|
220
|
+
<%= f.text_field :name, class: 'chat__room_name chat-room-form__input', placeholder: 'チャットルーム名を入力してください'%>
|
221
|
+
</div>
|
222
|
+
</div>
|
223
|
+
<div class='chat-room-form__field'>
|
224
|
+
</div>
|
225
|
+
<div class='chat-room-form__field'>
|
226
|
+
<div class='chat-room-form__field--left'>
|
227
|
+
<label class='chat-room-form__label' for='chat_room_チャットメンバー'>チャットメンバー</label>
|
228
|
+
</div>
|
229
|
+
<div class='chat-room-form__field--right'>
|
230
|
+
<select name="room[user_ids][]">
|
231
|
+
<option value="">チャットするユーザーを選択してください</option>
|
232
|
+
<% User.where.not(id: current_user.id).each do |user| %>
|
233
|
+
<option value=<%= user.id %>><%= user.name %></option>
|
234
|
+
<% end %>
|
235
|
+
</select>
|
236
|
+
<input name="room[user_ids][]" type="hidden" value=<%= current_user.id %>>
|
237
|
+
</div>
|
238
|
+
</div>
|
239
|
+
<div class='chat-room-form__field'>
|
240
|
+
<div class='chat-room-form__field--left'></div>
|
241
|
+
<div class='chat-room-form__field--right'>
|
242
|
+
<%= f.submit class: 'chat-room-form__action-btn'%>
|
243
|
+
</div>
|
244
|
+
</div>
|
245
|
+
<% end %>
|
246
|
+
</div>
|
247
|
+
```
|
2
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -41,7 +41,7 @@
|
|
41
41
|
def create
|
42
42
|
@room = Room.new(room_params)
|
43
43
|
if @room.save
|
44
|
-
redirect_to root_path
|
44
|
+
redirect_to root_path (←room作成後、root_pathには戻ります)
|
45
45
|
else
|
46
46
|
redirect_to new_room_path
|
47
47
|
end
|
1
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -191,5 +191,17 @@
|
|
191
191
|
|
192
192
|
|
193
193
|
### 補足情報(FW/ツールのバージョンなど)
|
194
|
+
【binding.pry後、@roomの値を確認した結果】
|
194
195
|
|
196
|
+
12: def create
|
197
|
+
13: @room = Room.new(room_params)
|
198
|
+
=> 14: binding.pry
|
195
|
-
|
199
|
+
15: if @room.save!
|
200
|
+
16: redirect_to root_path
|
201
|
+
17: else
|
202
|
+
18: redirect_to new_room_path
|
203
|
+
19: end
|
204
|
+
20: end
|
205
|
+
|
206
|
+
[1] pry(#<RoomsController>)> @room
|
207
|
+
=> #<Room:0x00007f855aaac5a8 id: nil, name: "新規作成", created_at: nil, updated_at: nil>
|