質問編集履歴
2
文の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
coordinationのnewビューに表示させているouterテーブルにある画像をcheck_boxで送信し、coordinationテーブルの外部キーouter_idに保存したいです。
|
4
4
|
|
5
5
|
ですがcoordinationの主キーは保存され、外部キーは保存されません。
|
6
|
+
なお、エラーは出てきません。
|
6
7
|
どなたか助けて欲しいです。
|
7
8
|
|
8
9
|
coordination/new.html.haml
|
1
画像からコードに変更しました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -5,21 +5,183 @@
|
|
5
5
|
ですがcoordinationの主キーは保存され、外部キーは保存されません。
|
6
6
|
どなたか助けて欲しいです。
|
7
7
|
|
8
|
+
coordination/new.html.haml
|
9
|
+
```
|
10
|
+
.lists
|
11
|
+
.cloth
|
12
|
+
服一覧
|
13
|
+
= form_for @outer do |f|
|
14
|
+
= f.label :"アウター", class: "outer-btn"
|
15
|
+
= f.file_field :outer, class: "file-btn"
|
16
|
+
= f.submit "追加", class: "add-btn"
|
17
|
+
%br
|
18
|
+
|
19
|
+
.bbbbb
|
20
|
+
V
|
21
|
+
- @outers.each do |outer|
|
22
|
+
= form_for @coordination do |q|
|
23
|
+
= image_tag outer.outer, class: 'apapap' if outer.present?
|
24
|
+
= q.text_field :coordination, class: "apapap"
|
25
|
+
= q.check_box :OuterId, as: :boolean, class: "apapap"
|
26
|
+
= q.submit "送信", class: "apapap"
|
27
|
+
|
28
|
+
```
|
29
|
+
coordination.controller
|
30
|
+
```
|
31
|
+
class CoordinationsController < ApplicationController
|
32
|
+
before_action :coordination_params ,only:[:create]
|
33
|
+
def index
|
34
|
+
@coordination = Coordination.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def new
|
38
|
+
@coordinations = Coordination.all
|
39
|
+
@outers = Outer.all
|
40
|
+
@inners = Inner.all
|
41
|
+
@bottoms = Bottom.all
|
42
|
+
@shoes = Shoe.all
|
43
|
+
@hats = Hat.all
|
44
|
+
@accessories = Accessory.all
|
45
|
+
@coordination = Coordination.new
|
46
|
+
@outer = Outer.new
|
47
|
+
@inner = Inner.new
|
48
|
+
@bottom = Bottom.new
|
49
|
+
@shoe = Shoe.new
|
50
|
+
@hat = Hat.new
|
51
|
+
@accessory = Accessory.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def create
|
55
|
+
@coordination = Coordination.create!(coordination_params)
|
56
|
+
end
|
57
|
+
def show
|
58
|
+
@coordinaitions = Coordination.all
|
59
|
+
end
|
60
|
+
|
61
|
+
def edit
|
62
|
+
@coordinations = Coordination.find(params[id])
|
63
|
+
@outers = Outer.find(params[id])
|
64
|
+
@inners = Inner.find(params[id])
|
65
|
+
@bottoms = Bottom.find(params[id])
|
66
|
+
@shoes = Shoe.find(params[id])
|
67
|
+
@hats = Hat.find(params[id])
|
68
|
+
@accessories = Accessory.find(params[id])
|
69
|
+
end
|
70
|
+
|
71
|
+
def update
|
72
|
+
|
73
|
+
@coordinations = Coordination.find(params[id])
|
74
|
+
|
75
|
+
@inners = Inner.find(params[id])
|
76
|
+
@bottoms = Bottom.find(params[id])
|
77
|
+
@shoes = Shoe.find(params[id])
|
78
|
+
@hats = Hat.find(params[id])
|
79
|
+
@accessories = Accessory.find(params[id])
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
def coordination_params
|
84
|
+
params.require(:coordination).permit(:season, :coordination, :inner_id, :bottom_id, :shoes_id, :hat_id, :accessory_id, :outer_id).merge(user_id: current_user.id)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
```
|
88
|
+
outerのコントローラー
|
89
|
+
```
|
90
|
+
class OutersController < ApplicationController
|
91
|
+
|
92
|
+
def index
|
93
|
+
@outers = Outer.all
|
94
|
+
end
|
95
|
+
def new
|
96
|
+
@outer = Outer.new
|
97
|
+
@outer.coordination.build
|
98
|
+
end
|
99
|
+
|
100
|
+
def create
|
101
|
+
Outer.create(outer_params)
|
102
|
+
end
|
103
|
+
|
104
|
+
def edit
|
105
|
+
# binding.pry
|
106
|
+
@outers = Outer.find(params[id])
|
107
|
+
end
|
108
|
+
def update
|
109
|
+
@outers = Outer.find(params[id])
|
110
|
+
end
|
111
|
+
def destroy
|
112
|
+
outer = Outer.find(params[:id])
|
113
|
+
outer.destroy
|
114
|
+
end
|
115
|
+
|
116
|
+
private
|
117
|
+
def outer_params
|
118
|
+
params.require(:outer).permit(:outer)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
```
|
8
122
|
coordinationのmigrationファイル
|
123
|
+
```
|
9
|
-
|
124
|
+
class CreateCoordinations < ActiveRecord::Migration[5.0]
|
125
|
+
def change
|
126
|
+
create_table :coordinations do |t|
|
127
|
+
t.string :season
|
128
|
+
t.string :coordination
|
129
|
+
t.timestamps
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
```
|
10
135
|
outerのmigrationファイル
|
136
|
+
```
|
11
|
-
|
137
|
+
class CreateOuters < ActiveRecord::Migration[5.0]
|
138
|
+
def change
|
139
|
+
create_table :outers do |t|
|
140
|
+
t.string :outer
|
141
|
+
t.string :image
|
142
|
+
t.timestamps
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
12
147
|
後付けした外部キーたち
|
148
|
+
```
|
149
|
+
class CreateChageCoordinations < ActiveRecord::Migration[5.0]
|
150
|
+
def change
|
151
|
+
create_table :chage_coordinations do |t|
|
152
|
+
add_reference :coordinations, :outer, foreign_key: true
|
153
|
+
add_reference :coordinations, :inner, foreign_key: true
|
154
|
+
add_reference :coordinations, :bottom, foreign_key: true
|
155
|
+
add_reference :coordinations, :shoes, foreign_key: true
|
13
|
-
|
156
|
+
add_reference :coordinations, :hat, foreign_key: true
|
157
|
+
add_reference :coordinations, :accessory, foreign_key: true
|
158
|
+
t.timestamps
|
159
|
+
t.timestamps
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
```
|
14
164
|
coordinationのモデル
|
165
|
+
```
|
166
|
+
class Coordination < ApplicationRecord
|
167
|
+
belongs_to :OuterId, optional: true
|
168
|
+
belongs_to :inner, optional: true
|
169
|
+
belongs_to :bottom, optional: true
|
170
|
+
belongs_to :shoe, optional: true
|
171
|
+
belongs_to :hat, optional: true
|
172
|
+
belongs_to :accessory, optional: true
|
173
|
+
belongs_to :user, optional: true
|
174
|
+
has_many :accessory_coordinations
|
175
|
+
mount_uploader :outer_id, ImageUploader
|
15
|
-
|
176
|
+
validates :coordination, presence: true
|
177
|
+
end
|
178
|
+
```
|
16
179
|
outerのモデル
|
180
|
+
```
|
17
|
-
|
181
|
+
class Outer < ApplicationRecord
|
182
|
+
belongs_to :user, optional: true
|
18
|
-
|
183
|
+
has_many :coordinations
|
184
|
+
mount_uploader :outer, ImageUploader
|
19
|
-
|
185
|
+
accepts_nested_attributes_for :coordinations
|
20
|
-
coordinationのコントローラーparams
|
21
|
-

|
22
|
-
|
186
|
+
end
|
23
|
-

|
24
|
-
|
187
|
+
```
|
25
|
-

|