質問編集履歴

2

文の追加

2020/01/21 08:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,8 @@
8
8
 
9
9
  ですがcoordinationの主キーは保存され、外部キーは保存されません。
10
10
 
11
+ なお、エラーは出てきません。
12
+
11
13
  どなたか助けて欲しいです。
12
14
 
13
15
 

1

画像からコードに変更しました。

2020/01/21 08:36

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -12,38 +12,362 @@
12
12
 
13
13
 
14
14
 
15
+ coordination/new.html.haml
16
+
17
+ ```
18
+
19
+ .lists
20
+
21
+ .cloth
22
+
23
+ 服一覧
24
+
25
+ = form_for @outer do |f|
26
+
27
+ = f.label :"アウター", class: "outer-btn"
28
+
29
+ = f.file_field :outer, class: "file-btn"
30
+
31
+ = f.submit "追加", class: "add-btn"
32
+
33
+ %br
34
+
35
+
36
+
37
+ .bbbbb
38
+
39
+ V
40
+
41
+ - @outers.each do |outer|
42
+
43
+ = form_for @coordination do |q|
44
+
45
+ = image_tag outer.outer, class: 'apapap' if outer.present?
46
+
47
+ = q.text_field :coordination, class: "apapap"
48
+
49
+ = q.check_box :OuterId, as: :boolean, class: "apapap"
50
+
51
+ = q.submit "送信", class: "apapap"
52
+
53
+
54
+
55
+ ```
56
+
57
+ coordination.controller
58
+
59
+ ```
60
+
61
+ class CoordinationsController < ApplicationController
62
+
63
+ before_action :coordination_params ,only:[:create]
64
+
65
+ def index
66
+
67
+ @coordination = Coordination.new
68
+
69
+ end
70
+
71
+
72
+
73
+ def new
74
+
75
+ @coordinations = Coordination.all
76
+
77
+ @outers = Outer.all
78
+
79
+ @inners = Inner.all
80
+
81
+ @bottoms = Bottom.all
82
+
83
+ @shoes = Shoe.all
84
+
85
+ @hats = Hat.all
86
+
87
+ @accessories = Accessory.all
88
+
89
+ @coordination = Coordination.new
90
+
91
+ @outer = Outer.new
92
+
93
+ @inner = Inner.new
94
+
95
+ @bottom = Bottom.new
96
+
97
+ @shoe = Shoe.new
98
+
99
+ @hat = Hat.new
100
+
101
+ @accessory = Accessory.new
102
+
103
+ end
104
+
105
+
106
+
107
+ def create
108
+
109
+ @coordination = Coordination.create!(coordination_params)
110
+
111
+ end
112
+
113
+ def show
114
+
115
+ @coordinaitions = Coordination.all
116
+
117
+ end
118
+
119
+
120
+
121
+ def edit
122
+
123
+ @coordinations = Coordination.find(params[id])
124
+
125
+ @outers = Outer.find(params[id])
126
+
127
+ @inners = Inner.find(params[id])
128
+
129
+ @bottoms = Bottom.find(params[id])
130
+
131
+ @shoes = Shoe.find(params[id])
132
+
133
+ @hats = Hat.find(params[id])
134
+
135
+ @accessories = Accessory.find(params[id])
136
+
137
+ end
138
+
139
+
140
+
141
+ def update
142
+
143
+
144
+
145
+ @coordinations = Coordination.find(params[id])
146
+
147
+
148
+
149
+ @inners = Inner.find(params[id])
150
+
151
+ @bottoms = Bottom.find(params[id])
152
+
153
+ @shoes = Shoe.find(params[id])
154
+
155
+ @hats = Hat.find(params[id])
156
+
157
+ @accessories = Accessory.find(params[id])
158
+
159
+ end
160
+
161
+
162
+
163
+ private
164
+
165
+ def coordination_params
166
+
167
+ params.require(:coordination).permit(:season, :coordination, :inner_id, :bottom_id, :shoes_id, :hat_id, :accessory_id, :outer_id).merge(user_id: current_user.id)
168
+
169
+ end
170
+
171
+ end
172
+
173
+ ```
174
+
175
+ outerのコントローラー
176
+
177
+ ```
178
+
179
+ class OutersController < ApplicationController
180
+
181
+
182
+
183
+ def index
184
+
185
+ @outers = Outer.all
186
+
187
+ end
188
+
189
+ def new
190
+
191
+ @outer = Outer.new
192
+
193
+ @outer.coordination.build
194
+
195
+ end
196
+
197
+
198
+
199
+ def create
200
+
201
+ Outer.create(outer_params)
202
+
203
+ end
204
+
205
+
206
+
207
+ def edit
208
+
209
+ # binding.pry
210
+
211
+ @outers = Outer.find(params[id])
212
+
213
+ end
214
+
215
+ def update
216
+
217
+ @outers = Outer.find(params[id])
218
+
219
+ end
220
+
221
+ def destroy
222
+
223
+ outer = Outer.find(params[:id])
224
+
225
+ outer.destroy
226
+
227
+ end
228
+
229
+
230
+
231
+ private
232
+
233
+ def outer_params
234
+
235
+ params.require(:outer).permit(:outer)
236
+
237
+ end
238
+
239
+ end
240
+
241
+ ```
242
+
15
243
  coordinationのmigrationファイル
16
244
 
245
+ ```
246
+
17
- ![イメージ説明](4262577e6d1ba59ff377bbaf0df33c18.png)
247
+ class CreateCoordinations < ActiveRecord::Migration[5.0]
248
+
249
+ def change
250
+
251
+ create_table :coordinations do |t|
252
+
253
+ t.string :season
254
+
255
+ t.string :coordination
256
+
257
+ t.timestamps
258
+
259
+ end
260
+
261
+ end
262
+
263
+ end
264
+
265
+
266
+
267
+ ```
18
268
 
19
269
  outerのmigrationファイル
20
270
 
271
+ ```
272
+
21
- ![イメージ説明](0d0cdc3c89b092c9ddbf381c45175c79.png)
273
+ class CreateOuters < ActiveRecord::Migration[5.0]
274
+
275
+ def change
276
+
277
+ create_table :outers do |t|
278
+
279
+ t.string :outer
280
+
281
+ t.string :image
282
+
283
+ t.timestamps
284
+
285
+ end
286
+
287
+ end
288
+
289
+ end
290
+
291
+ ```
22
292
 
23
293
  後付けした外部キーたち
24
294
 
295
+ ```
296
+
297
+ class CreateChageCoordinations < ActiveRecord::Migration[5.0]
298
+
299
+ def change
300
+
301
+ create_table :chage_coordinations do |t|
302
+
303
+ add_reference :coordinations, :outer, foreign_key: true
304
+
305
+ add_reference :coordinations, :inner, foreign_key: true
306
+
307
+ add_reference :coordinations, :bottom, foreign_key: true
308
+
309
+ add_reference :coordinations, :shoes, foreign_key: true
310
+
25
- ![イメージ説明](526971e6219b3991d61f5674939225ec.png)
311
+ add_reference :coordinations, :hat, foreign_key: true
312
+
313
+ add_reference :coordinations, :accessory, foreign_key: true
314
+
315
+ t.timestamps
316
+
317
+ t.timestamps
318
+
319
+ end
320
+
321
+ end
322
+
323
+ end
324
+
325
+ ```
26
326
 
27
327
  coordinationのモデル
28
328
 
329
+ ```
330
+
331
+ class Coordination < ApplicationRecord
332
+
333
+ belongs_to :OuterId, optional: true
334
+
335
+ belongs_to :inner, optional: true
336
+
337
+ belongs_to :bottom, optional: true
338
+
339
+ belongs_to :shoe, optional: true
340
+
341
+ belongs_to :hat, optional: true
342
+
343
+ belongs_to :accessory, optional: true
344
+
345
+ belongs_to :user, optional: true
346
+
347
+ has_many :accessory_coordinations
348
+
349
+ mount_uploader :outer_id, ImageUploader
350
+
29
- ![イメージ説明](dc0e941cd1f9977fe7e0ce8564d2c80e.png)
351
+ validates :coordination, presence: true
352
+
353
+ end
354
+
355
+ ```
30
356
 
31
357
  outerのモデル
32
358
 
359
+ ```
360
+
33
- ![![イメージ説明](2fd4adc674c7b13b6f832c9732dce9d9.png)
361
+ class Outer < ApplicationRecord
362
+
34
-
363
+ belongs_to :user, optional: true
364
+
35
- coordinationのコントローラー
365
+ has_many :coordinations
366
+
36
-
367
+ mount_uploader :outer, ImageUploader
368
+
37
- ![イメージ説明](92bc8eef34d4ac8b0220ddb0e15d5050.png)
369
+ accepts_nested_attributes_for :coordinations
38
-
39
- coordinationのコントローラーparams
370
+
40
-
41
- ![イメージ説明](11bfcc67d5c09ac43aae74b7f9465be4.png)
42
-
43
- outerのコントローラー
371
+ end
44
-
45
- ![イメージ説明](9fff8194df5093e7e451eb35d461b838.png)
372
+
46
-
47
- coordinationのnew.haml
373
+ ```
48
-
49
- ![イメージ説明](08f63fb921d8771c881543c8705754b2.png)