質問編集履歴
3
Listscontrollerを記述
test
CHANGED
File without changes
|
test
CHANGED
@@ -326,6 +326,176 @@
|
|
326
326
|
|
327
327
|
|
328
328
|
|
329
|
+
```Ruby
|
330
|
+
|
331
|
+
class ListsController < ApplicationController
|
332
|
+
|
333
|
+
before_action :set_list, only: [:show, :edit, :update, :destroy]
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
# GET /lists
|
338
|
+
|
339
|
+
# GET /lists.json
|
340
|
+
|
341
|
+
def index
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
#@list = List.find(params[:id])
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
@lists= List.where(user_id: current_user.id).order("start_time ASC")
|
350
|
+
|
351
|
+
end
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
# GET /lists/1
|
356
|
+
|
357
|
+
# GET /lists/1.json
|
358
|
+
|
359
|
+
def show
|
360
|
+
|
361
|
+
end
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
# GET /lists/new
|
366
|
+
|
367
|
+
def new
|
368
|
+
|
369
|
+
@list = List.new
|
370
|
+
|
371
|
+
end
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
# GET /lists/1/edit
|
376
|
+
|
377
|
+
def edit
|
378
|
+
|
379
|
+
end
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
# POST /lists
|
384
|
+
|
385
|
+
# POST /lists.json
|
386
|
+
|
387
|
+
def create
|
388
|
+
|
389
|
+
@list = List.new(list_params)
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
@list.user_id = current_user.id
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
#@list.tag_list.user_id = current_user.id
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
|
402
|
+
|
403
|
+
respond_to do |format|
|
404
|
+
|
405
|
+
if @list.save
|
406
|
+
|
407
|
+
format.html { redirect_to @list, notice: 'List was successfully created.' }
|
408
|
+
|
409
|
+
format.json { render :show, status: :created, location: @list }
|
410
|
+
|
411
|
+
else
|
412
|
+
|
413
|
+
format.html { render :new }
|
414
|
+
|
415
|
+
format.json { render json: @list.errors, status: :unprocessable_entity }
|
416
|
+
|
417
|
+
end
|
418
|
+
|
419
|
+
end
|
420
|
+
|
421
|
+
end
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
# PATCH/PUT /lists/1
|
426
|
+
|
427
|
+
# PATCH/PUT /lists/1.json
|
428
|
+
|
429
|
+
def update
|
430
|
+
|
431
|
+
respond_to do |format|
|
432
|
+
|
433
|
+
if @list.update(list_params)
|
434
|
+
|
435
|
+
format.html { redirect_to @list, notice: 'List was successfully updated.' }
|
436
|
+
|
437
|
+
format.json { render :show, status: :ok, location: @list }
|
438
|
+
|
439
|
+
else
|
440
|
+
|
441
|
+
format.html { render :edit }
|
442
|
+
|
443
|
+
format.json { render json: @list.errors, status: :unprocessable_entity }
|
444
|
+
|
445
|
+
end
|
446
|
+
|
447
|
+
end
|
448
|
+
|
449
|
+
end
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
# DELETE /lists/1
|
454
|
+
|
455
|
+
# DELETE /lists/1.json
|
456
|
+
|
457
|
+
def destroy
|
458
|
+
|
459
|
+
@list.destroy
|
460
|
+
|
461
|
+
respond_to do |format|
|
462
|
+
|
463
|
+
format.html { redirect_to tag_lists_url, notice: 'List was successfully destroyed.' }
|
464
|
+
|
465
|
+
format.json { head :no_content }
|
466
|
+
|
467
|
+
end
|
468
|
+
|
469
|
+
end
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
private
|
474
|
+
|
475
|
+
# Use callbacks to share common setup or constraints between actions.
|
476
|
+
|
477
|
+
def set_list
|
478
|
+
|
479
|
+
@list = List.find(params[:id])
|
480
|
+
|
481
|
+
end
|
482
|
+
|
483
|
+
|
484
|
+
|
485
|
+
# Never trust parameters from the scary internet, only allow the white list through.
|
486
|
+
|
487
|
+
def list_params
|
488
|
+
|
489
|
+
params.require(:list).permit(:content,:start_time, tag_ids:[])
|
490
|
+
|
491
|
+
end
|
492
|
+
|
493
|
+
end
|
494
|
+
|
495
|
+
```
|
496
|
+
|
497
|
+
|
498
|
+
|
329
499
|
|
330
500
|
|
331
501
|
|
2
タイトル
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Railsで、中間テーブルのカラムに
|
1
|
+
Railsで、中間テーブルのカラムに他のテーブルからカラムの値を参照したい
|
test
CHANGED
File without changes
|
1
質問文のコード部分の書き方
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,14 +1,154 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
+
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
Tagモデル、ListモデルをTagListモデルを中間テーブルとして関連付けしています。
|
8
|
+
|
9
|
+
ログイン中のユーザーの作成したもののみをtag_lists_controllerで@tag_lists = TagList.where(user_id: current_user.id)とすることでtaglistsの一覧ページで表示させたいのですが、
|
10
|
+
|
11
|
+
コマンドラインで確認したところTagList.user_idがnilになっているためうまくできません。
|
12
|
+
|
13
|
+
なので、今回実現したいことはTagList.user_idにcurrent_user.idを代入できるようにすることです。
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
各テーブルのカラムは以下のようになっています。
|
18
|
+
|
19
|
+
```Ruby
|
20
|
+
|
21
|
+
create_table "tags", force: :cascade do |t|
|
22
|
+
|
23
|
+
t.string "content"
|
24
|
+
|
25
|
+
t.datetime "created_at", null: false
|
26
|
+
|
27
|
+
t.datetime "updated_at", null: false
|
28
|
+
|
29
|
+
t.integer "user_id"
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
create_table "lists", force: :cascade do |t|
|
36
|
+
|
37
|
+
t.string "content"
|
38
|
+
|
39
|
+
t.datetime "created_at", null: false
|
40
|
+
|
41
|
+
t.datetime "updated_at", null: false
|
42
|
+
|
43
|
+
t.integer "user_id"
|
44
|
+
|
45
|
+
t.date "start_time"
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
create_table "tag_lists", force: :cascade do |t|
|
52
|
+
|
53
|
+
t.integer "tag_id"
|
54
|
+
|
55
|
+
t.integer "list_id"
|
56
|
+
|
57
|
+
t.datetime "created_at", null: false
|
58
|
+
|
59
|
+
t.datetime "updated_at", null: false
|
60
|
+
|
61
|
+
t.integer "user_id"
|
62
|
+
|
63
|
+
t.index ["list_id"], name: "index_tag_lists_on_list_id"
|
64
|
+
|
65
|
+
t.index ["tag_id"], name: "index_tag_lists_on_tag_id"
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
create_table "users", force: :cascade do |t|
|
72
|
+
|
73
|
+
t.string "email", default: "", null: false
|
74
|
+
|
75
|
+
t.string "encrypted_password", default: "", null: false
|
76
|
+
|
77
|
+
t.string "reset_password_token"
|
78
|
+
|
79
|
+
t.datetime "reset_password_sent_at"
|
80
|
+
|
81
|
+
t.datetime "remember_created_at"
|
82
|
+
|
83
|
+
t.datetime "created_at", null: false
|
84
|
+
|
85
|
+
t.datetime "updated_at", null: false
|
86
|
+
|
87
|
+
t.index ["email"], name: "index_users_on_email", unique: true
|
88
|
+
|
89
|
+
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
|
3
95
|
```
|
4
96
|
|
5
97
|
|
6
98
|
|
99
|
+
また、各モデルのアソシエーションは以下のようになっています。
|
100
|
+
|
101
|
+
```Ruby
|
102
|
+
|
103
|
+
class Tag < ApplicationRecord
|
104
|
+
|
105
|
+
belongs_to :user, optional: true
|
106
|
+
|
107
|
+
has_many :tag_lists, dependent: :destroy
|
108
|
+
|
109
|
+
has_many :lists, :through => :tag_lists
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
class List < ApplicationRecord
|
116
|
+
|
117
|
+
belongs_to :user, optional: true
|
118
|
+
|
119
|
+
has_many :tag_lists, dependent: :destroy
|
120
|
+
|
121
|
+
has_many :tags, :through => :tag_lists
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
|
7
|
-
Tag
|
127
|
+
class TagList < ApplicationRecord
|
8
|
-
|
128
|
+
|
9
|
-
|
129
|
+
belongs_to :user, optional: true
|
130
|
+
|
10
|
-
|
131
|
+
belongs_to :tag, optional: true
|
132
|
+
|
11
|
-
|
133
|
+
belongs_to :list, optional: true
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
class User < ApplicationRecord
|
140
|
+
|
141
|
+
has_many :diaries
|
142
|
+
|
143
|
+
has_many :tags
|
144
|
+
|
145
|
+
has_many :lists
|
146
|
+
|
147
|
+
has_many :tag_lists
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
```
|
12
152
|
|
13
153
|
|
14
154
|
|
@@ -16,35 +156,177 @@
|
|
16
156
|
|
17
157
|
### 発生している問題・エラーメッセージ
|
18
158
|
|
159
|
+
エラーメッセージは表示されていません。
|
160
|
+
|
19
161
|
コマンドラインでrails cをしてTagList.allと入力するとuser_id: nilと表示されます。
|
20
162
|
|
21
163
|
本来はこのnilの部分にcurrent_user.id(ログイン中のユーザーのid)が入るようにしたいです。
|
22
164
|
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
### 該当のソースコード
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
```Ruby
|
174
|
+
|
175
|
+
class TagListsController < ApplicationController
|
176
|
+
|
177
|
+
before_action :set_tag_list, only: [:show, :edit, :update, :destroy]
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
def index
|
182
|
+
|
183
|
+
@tag_lists = TagList.where(user_id: current_user.id)
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
# GET /tag_lists/1
|
190
|
+
|
191
|
+
# GET /tag_lists/1.json
|
192
|
+
|
193
|
+
def show
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
# GET /tag_lists/new
|
200
|
+
|
201
|
+
def new
|
202
|
+
|
203
|
+
@tag_list = TagList.new
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# GET /tag_lists/1/edit
|
210
|
+
|
211
|
+
def edit
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
# POST /tag_lists
|
218
|
+
|
219
|
+
# POST /tag_lists.json
|
220
|
+
|
221
|
+
def create
|
222
|
+
|
223
|
+
@tag_list = TagList.new(tag_list_params)
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
@tag_list.user_id = current_user.id
|
228
|
+
|
229
|
+
#@tag_list.user_id = tag_list.list.user_id
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
respond_to do |format|
|
234
|
+
|
235
|
+
if @tag_list.save
|
236
|
+
|
237
|
+
format.html { redirect_to @tag_list, notice: 'Tag list was successfully created.' }
|
238
|
+
|
239
|
+
format.json { render :show, status: :created, location: @tag_list }
|
240
|
+
|
241
|
+
else
|
242
|
+
|
243
|
+
format.html { render :new }
|
244
|
+
|
245
|
+
format.json { render json: @tag_list.errors, status: :unprocessable_entity }
|
246
|
+
|
247
|
+
end
|
248
|
+
|
249
|
+
end
|
250
|
+
|
251
|
+
end
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
# PATCH/PUT /tag_lists/1
|
256
|
+
|
257
|
+
# PATCH/PUT /tag_lists/1.json
|
258
|
+
|
259
|
+
def update
|
260
|
+
|
261
|
+
respond_to do |format|
|
262
|
+
|
263
|
+
if @tag_list.update(tag_list_params)
|
264
|
+
|
265
|
+
format.html { redirect_to @tag_list, notice: 'Tag list was successfully updated.' }
|
266
|
+
|
267
|
+
format.json { render :show, status: :ok, location: @tag_list }
|
268
|
+
|
269
|
+
else
|
270
|
+
|
271
|
+
format.html { render :edit }
|
272
|
+
|
273
|
+
format.json { render json: @tag_list.errors, status: :unprocessable_entity }
|
274
|
+
|
275
|
+
end
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
end
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
# DELETE /tag_lists/1
|
284
|
+
|
285
|
+
# DELETE /tag_lists/1.json
|
286
|
+
|
287
|
+
def destroy
|
288
|
+
|
289
|
+
@tag_list.destroy
|
290
|
+
|
291
|
+
respond_to do |format|
|
292
|
+
|
293
|
+
format.html { redirect_to tag_lists_url, notice: 'Tag list was successfully destroyed.' }
|
294
|
+
|
295
|
+
format.json { head :no_content }
|
296
|
+
|
297
|
+
end
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
private
|
304
|
+
|
305
|
+
# Use callbacks to share common setup or constraints between actions.
|
306
|
+
|
307
|
+
def set_tag_list
|
308
|
+
|
309
|
+
@tag_list = TagList.find(params[:id])
|
310
|
+
|
311
|
+
end
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
# Never trust parameters from the scary internet, only allow the white list through.
|
316
|
+
|
317
|
+
def tag_list_params
|
318
|
+
|
319
|
+
params.require(:tag_list).permit(:tag_id, :list_id)
|
320
|
+
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
|
23
325
|
```
|
24
326
|
|
25
|
-
|
327
|
+
|
26
|
-
|
27
|
-
|
328
|
+
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
329
|
+
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
```ここに言語名を入力
|
36
|
-
|
37
|
-
ソースコード
|
38
|
-
|
39
|
-
```
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
### 試したこと
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
ここに問題に対して試したことを記載してください。
|
48
330
|
|
49
331
|
|
50
332
|
|
@@ -52,4 +334,4 @@
|
|
52
334
|
|
53
335
|
|
54
336
|
|
55
|
-
|
337
|
+
rails 5.1.7
|