如何使用carrierwave上传多个文件并创建多个记录

世居

我正在尝试修改我的图像上传程序,以便同时上传多个图像。目前我可以上传多张图片,但它只创建了一个记录。如何实现一次提交创建多条记录?

我不想通过关联,而是能够直接上传多个图像。

我目前有一个模型:

class Diapo < ApplicationRecord
  belongs_to :album
  mount_uploaders :file_name, DiapoUploader
end

我的控制器:

class Admin::DiaposController < ApplicationController

  def new
    @diapo = Diapo.new
    @page_title = 'Create new Diapo'
  end

  def create
    @diapo = Diapo.new(diapo_params)
    if @diapo.save
      flash[:notice] = 'Diapo #{@diapo.file_name} was successfully created.
      redirect_to action: :index
    else
      @page_title = 'Create new Diapo'
      render action: :new
    end
  end

.
.
.

  private

  def diapo_params
    params.require(:diapo).permit({file_name: []}, :title, :alt, :author, :copyright, :album_id)
  end
end

以我的形式:

<%= f.file_field :file_name, multiple: true, class: 'form-field' %>

目前我收到一个帖子

Started POST "/admin/diapos" for ::1 at 2019-12-18 10:45:19 +0100
Processing by Admin::DiaposController#create as HTML
  Parameters: {"authenticity_token"=>"something==", "diapo"=>{"file_name"=>[#<ActionDispatch::Http::UploadedFile:0x00007f9062b51a40 @tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20191218-2329-7kffxo.jpg>, @original_filename="1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"diapo[file_name][]\"; filename=\"1.jpg\"\r\nContent-Type: image/jpeg\r\n">, #<ActionDispatch::Http::UploadedFile:0x00007f9062b519f0 @tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20191218-2329-o5qzhe.jpg>, @original_filename="2.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"diapo[file_name][]\"; filename=\"2.jpg\"\r\nContent-Type: image/jpeg\r\n">], "alt"=>"Test", "author_ids"=>"", "album_id"=>"124", "copyright"=>"Test"}, "commit"=>"Save"}
  Admin Load (0.4ms)  SELECT 'admins'.* FROM 'admins' WHERE 'admins'.'id' = 1 ORDER BY 'admins'.'id' ASC LIMIT 1
Unpermitted parameter: :author_ids
(0.2ms)  BEGIN
  ↳ app/controllers/admin/diapos_controller.rb:14:in `create'
  Album Load (0.3ms)  SELECT `albums`.* FROM `albums` WHERE `albums`.`id` = 124 LIMIT 1
  ↳ app/controllers/admin/diapos_controller.rb:14:in `create'
  Diapo Create (0.2ms)  INSERT INTO `diapos` (`file_name`, `alt`, `copyright`, `album_id`, `created_at`, `updated_at`) VALUES ('[\"1.jpg\", \"2.jpg\"]', 'Test', 'Test', 124, '2019-12-18 09:45:34.406410', '2019-12-18 09:45:34.406410')
  ↳ app/controllers/admin/diapos_controller.rb:14:in `create'
   (6.5ms)  COMMIT
  ↳ app/controllers/admin/diapos_controller.rb:14:in `create'
Redirected to http://localhost:3000/admin/diapos
Completed 302 Found in 15350ms (ActiveRecord: 7.5ms | Allocations: 75512)

并且创建了 1 条记录,其在文件名字段中具有文件名数组。

我怀疑我可以在控制器中还是通过 javascript 来完成?我该怎么做?

先感谢您!

代码_aks

您必须使用has_many关系来实现您的要求要保存多个记录,您可能具有如下所示的父子关系。

例如:- Post 是一个父表,并将其图像保存为 post_attachments,即子表

class Post < ActiveRecord::Base
 has_many :post_attachments
 accepts_nested_attributes_for :post_attachments
end

class PostAttachment < ActiveRecord::Base
   mount_uploader :avatar, AvatarUploader
   belongs_to :post
end

这是您如何实现为父记录保存多个文件

有关更多详细信息,请参阅这篇文章,它可能对您有所帮助。

https://kolosek.com/carrierwave-upload-multiple-images/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章