コントローラでzipファイルを取得したあと、tempfileに一時的に解凍・保存してproduct_images1 ~ product_images10カラムに自動的に振り分けてレコードを作成する、という機能を作りたいと思っています。
ruby
1# products_controller.rb 2def create 3 @product = Product.new(product_params) 4 5 zipfile = product_params[:zip] 6 Zip::File.open(zipfile.path) do |zip| 7 num = 0 8 zip.each do |entry| 9 num = num + 1 10 return if num > 30 11 ext = File.extname(entry.name) #拡張子を取得 12 # 隠しファイルやゴミファイルは飛ばす 13 next if ext.blank? || File.basename(entry.name).count(".") > 1 14 # 一時ファイルを作る 15 Tempfile.open(["tempProductImageFile", ext]) do |t| 16 entry.extract(t.path) { true } 17 # カラムに値を入れる 18 @product.send("products_image#{num}=", t) 19 end 20 p "ccc" 21 end 22 p "ddd" 23 end 24 25 p "jjj" 26 respond_to do |format| 27 if @product.save 28 p "kkk" 29 format.html { redirect_to [:manage, @product], notice: "#{@product.building_name}を作成しました。" } 30 format.json { render :show, status: :created, location: @product } 31 else 32 format.html { render :new } 33 format.json { render json: @product.errors, status: :unprocessable_entity } 34 end 35 end 36 end 37 end
##結果
上記のようなコードを書いてこのアクションを起こしたところ、
Missing template manage/products/create, manage/application/create
というエラーが出ました。
ご教示いただけることがあれば、何卒よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー