回答編集履歴
5
添削
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
```slim
|
12
12
|
|
13
|
-
= form_with model: @parent,
|
13
|
+
= form_with model: @parent, local: true do |f|
|
14
14
|
|
15
15
|
= f.fields_for @child do |a|
|
16
16
|
|
4
form_forでも検証
test
CHANGED
@@ -10,7 +10,19 @@
|
|
10
10
|
|
11
11
|
```slim
|
12
12
|
|
13
|
-
= form_with model: @parent,
|
13
|
+
= form_with model: @parent, method: :post, local: true do |f|
|
14
|
+
|
15
|
+
= f.fields_for @child do |a|
|
16
|
+
|
17
|
+
= a.file_field :images, multiple: true, type: :file
|
18
|
+
|
19
|
+
= f.submit
|
20
|
+
|
21
|
+
```
|
22
|
+
|
23
|
+
```slim
|
24
|
+
|
25
|
+
= form_for @parent do |f|
|
14
26
|
|
15
27
|
= f.fields_for @child do |a|
|
16
28
|
|
3
修正
test
CHANGED
@@ -30,13 +30,13 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
parent_params = params.require(:user).permit(absence: { image: [] })
|
33
|
+
parent_params = params.require(:user).permit(absence: { images: [] })
|
34
34
|
|
35
35
|
#=> <ActionController::Parameters {"child"=><ActionController::Parameters {"images"=>[#<ActionDispatch::Http::UploadedFile:0x00007fa5b370a138 ...>, #<ActionDispatch::Http::UploadedFile:0x00007fa5b370a0e8 ...>]} permitted: true>} permitted: true>
|
36
36
|
|
37
37
|
|
38
38
|
|
39
|
-
parent_params[:child][:image]
|
39
|
+
parent_params[:child][:images]
|
40
40
|
|
41
41
|
#=> [#<ActionDispatch::Http::UploadedFile:0x00007fa5b370a138 ...">, #<ActionDispatch::Http::UploadedFile:0x00007fa5b370a0e8 ...">]
|
42
42
|
|
2
修正
test
CHANGED
@@ -1 +1,53 @@
|
|
1
|
-
|
1
|
+
`multiple: true`を指定した場合、配列のパラメータが送られてくるようなので
|
2
|
+
|
3
|
+
`multiple: true`を使うのであれば、パラメータを受け取ったあと、別々に保存する処理が必要そうです。
|
4
|
+
|
5
|
+
今回の質問内容とは異なるので割愛します。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
試しにフォームを作って見ました。
|
10
|
+
|
11
|
+
```slim
|
12
|
+
|
13
|
+
= form_with model: @parent, url: user_absences_url, method: :post, local: true do |f|
|
14
|
+
|
15
|
+
= f.fields_for @child do |a|
|
16
|
+
|
17
|
+
= a.file_field :images, multiple: true, type: :file
|
18
|
+
|
19
|
+
= f.submit
|
20
|
+
|
21
|
+
```
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
|
25
|
+
# byebug
|
26
|
+
|
27
|
+
params
|
28
|
+
|
29
|
+
#=> <ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"...", "parent"=>{"child"=>{"images"=>[#<ActionDispatch::Http::UploadedFile:0x00007fa5b370a138 ...>, #<ActionDispatch::Http::UploadedFile:0x00007fa5b370a0e8 ...>]}}, "commit"=>"登録する", "controller"=>"parent", "action"=>"create", "parent_id"=>"1"} permitted: false>
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
parent_params = params.require(:user).permit(absence: { image: [] })
|
34
|
+
|
35
|
+
#=> <ActionController::Parameters {"child"=><ActionController::Parameters {"images"=>[#<ActionDispatch::Http::UploadedFile:0x00007fa5b370a138 ...>, #<ActionDispatch::Http::UploadedFile:0x00007fa5b370a0e8 ...>]} permitted: true>} permitted: true>
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
parent_params[:child][:image]
|
40
|
+
|
41
|
+
#=> [#<ActionDispatch::Http::UploadedFile:0x00007fa5b370a138 ...">, #<ActionDispatch::Http::UploadedFile:0x00007fa5b370a0e8 ...">]
|
42
|
+
|
43
|
+
```
|
44
|
+
|
45
|
+
`'0':`のようなキーは入り込みませんね。
|
46
|
+
|
47
|
+
どちらにせよ`@product = Product.new(product_params)`な形で登録するのは難しいと思います。
|
48
|
+
|
49
|
+
(配列で渡されるimageを分解して別々に登録する処理が必要なため)
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
`'0':`のようなキーが入りこむ原因がわかりましたら回答を編集しておきます。
|
1
.
test
CHANGED
@@ -1,11 +1 @@
|
|
1
|
-
|
1
|
+
............
|
2
|
-
|
3
|
-
= form_for @product, html: {class: "new-main__sell"} do |f|
|
4
|
-
|
5
|
-
%label.image-upload-dropbox.have-item-0
|
6
|
-
|
7
|
-
= f.fields_for :item_images do |i|
|
8
|
-
|
9
|
-
= i.file_field :name, multiple: true, class: 'image-upload-dropfile hidden', type: 'file'
|
10
|
-
|
11
|
-
```
|