質問編集履歴
2
不足があったため
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
[Ruby On Rails]
|
1
|
+
[Ruby On Rails]ZIPファイルを一時ファイルに解凍・保存して、順番にカラムに入れたい
|
test
CHANGED
@@ -8,29 +8,69 @@
|
|
8
8
|
|
9
9
|
def create
|
10
10
|
|
11
|
-
@product = Product.new
|
11
|
+
@product = Product.new(product_params)
|
12
12
|
|
13
|
-
zipfile = product_params[:zip]
|
14
13
|
|
15
|
-
Zip::File.open(zipfile.path) do |zip|
|
16
14
|
|
17
|
-
zip
|
15
|
+
zipfile = product_params[:zip]
|
18
16
|
|
19
|
-
|
17
|
+
Zip::File.open(zipfile.path) do |zip|
|
20
18
|
|
21
19
|
num = 0
|
22
20
|
|
23
|
-
temp = Tempfile.open(["tempProductImageFile", ext])
|
24
|
-
|
25
|
-
|
21
|
+
zip.each do |entry|
|
26
22
|
|
27
23
|
num = num + 1
|
28
24
|
|
29
|
-
return if num >
|
25
|
+
return if num > 30
|
30
26
|
|
31
|
-
e
|
27
|
+
ext = File.extname(entry.name) #拡張子を取得
|
32
28
|
|
29
|
+
# 隠しファイルやゴミファイルは飛ばす
|
30
|
+
|
31
|
+
next if ext.blank? || File.basename(entry.name).count(".") > 1
|
32
|
+
|
33
|
+
# 一時ファイルを作る
|
34
|
+
|
35
|
+
Tempfile.open(["tempProductImageFile", ext]) do |t|
|
36
|
+
|
37
|
+
entry.extract(t.path) { true }
|
38
|
+
|
39
|
+
# カラムに値を入れる
|
40
|
+
|
33
|
-
@product.send("products_image#{num}=", t)
|
41
|
+
@product.send("products_image#{num}=", t)
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
p "ccc"
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
p "ddd"
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
p "jjj"
|
56
|
+
|
57
|
+
respond_to do |format|
|
58
|
+
|
59
|
+
if @product.save
|
60
|
+
|
61
|
+
p "kkk"
|
62
|
+
|
63
|
+
format.html { redirect_to [:manage, @product], notice: "#{@product.building_name}を作成しました。" }
|
64
|
+
|
65
|
+
format.json { render :show, status: :created, location: @product }
|
66
|
+
|
67
|
+
else
|
68
|
+
|
69
|
+
format.html { render :new }
|
70
|
+
|
71
|
+
format.json { render json: @product.errors, status: :unprocessable_entity }
|
72
|
+
|
73
|
+
end
|
34
74
|
|
35
75
|
end
|
36
76
|
|
@@ -38,53 +78,21 @@
|
|
38
78
|
|
39
79
|
end
|
40
80
|
|
41
|
-
end
|
42
|
-
|
43
81
|
```
|
44
82
|
|
45
83
|
##結果
|
46
84
|
|
47
85
|
|
48
86
|
|
49
|
-
上記のようなコードを書
|
87
|
+
上記のようなコードを書いてこのアクションを起こしたところ、
|
50
88
|
|
51
|
-
temp
|
89
|
+
Missing template manage/products/create, manage/application/create
|
52
90
|
|
53
|
-
<Tempfile:/var/folders/zx/41gt886555n2m1jp6mqb0kkh0000gn/T/tempProductImageFile20181218-89055-voggar.jpg>
|
54
|
-
|
55
|
-
|
91
|
+
というエラーが出ました。
|
56
92
|
|
57
93
|
|
58
94
|
|
59
|
-
## 試したこと
|
60
95
|
|
61
|
-
ただ、以下のように実装すると、tempfileが一つ一つイテレータで回されているのがわかります。
|
62
|
-
|
63
|
-
```ruby
|
64
|
-
|
65
|
-
# products_controller_rb
|
66
|
-
|
67
|
-
Tempfile.open(["tempProductImageFile", ext]).each do |t|
|
68
|
-
|
69
|
-
entry.extract(t.path) { true }
|
70
|
-
|
71
|
-
@product.send("products_image#{num}=", t)
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
```
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
ですが、#{num}に入れるメソッドを探したのですが見つけられませんでした。
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
temp = Tempfile.open(["tempProductImageFile", ext])としてイテレータで回すか、
|
84
|
-
|
85
|
-
もしくは
|
86
|
-
|
87
|
-
Tempfile.open(["tempProductImageFile", ext]).each do |t| と end の間でそれぞれのファイルのインデックス番号を取得するなどして#{num}の中に入れて、一度にproduct_images1 ~ product_images10カラムにレコードを入れたいです。
|
88
96
|
|
89
97
|
|
90
98
|
|
1
内容に不足があったため
test
CHANGED
File without changes
|
test
CHANGED
@@ -58,7 +58,7 @@
|
|
58
58
|
|
59
59
|
## 試したこと
|
60
60
|
|
61
|
-
ただ、以下のように実装すると、
|
61
|
+
ただ、以下のように実装すると、tempfileが一つ一つイテレータで回されているのがわかります。
|
62
62
|
|
63
63
|
```ruby
|
64
64
|
|