ruby-on-rails - rails : multi-file upload produces one empty entry

标签 ruby-on-rails carrierwave

我正在尝试创建某种嵌套文件上传。我有图像和相册,它们通过模型 AlbumImages 相关联。图像可以是相册的一部分。上传通过 Carrierwave 进行。

我希望能够通过两种方式上传图片:

  1. 直接通过图片
  2. 通过相册

通过相册上传图片时,应将它们上传并插入到 Images 和 AlbumImages 表中。

我可以通过图片上传单张图片,但令我惊讶的是,我当前通过相册上传多张图片的实现会产生 2 个单独的数据条目。 (这曾经在 Rails 3 下工作)第一个条目是空的,第二个是我的目标。

Started PATCH "/admin/albums/38" for ::1 at 2022-05-20 11:18:44 +0200
Processing by Admin::AlbumsController#update as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "album"=>{"images_attributes"=>[{"file_name"=>""}, {"file_name"=>#<ActionDispatch::Http::UploadedFile:0x00007f8f75185c70 @tempfile=#<Tempfile:/var/folders/yg/pfjwzpkx5wq9d27svk760h0h0000gn/T/RackMultipart20220520-19476-kto7za.jpg>, @original_filename="some-image.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"album[images_attributes][][file_name]\"; filename=\"some-image.jpg\"\r\nContent-Type: image/jpeg\r\n">, "author"=>"Some Author", "copyright"=>"Someone", "funds_ids"=>["1", "6"]}]}, "commit"=>"Save", "id"=>"38"}
  Admin Load (0.3ms)  SELECT `admins`.* FROM `admins` WHERE `admins`.`id` = 1 ORDER BY `admins`.`id` ASC LIMIT 1
  Album Load (0.2ms)  SELECT `albums`.* FROM `albums` WHERE `albums`.`id` = 38 LIMIT 1
  ↳ app/controllers/admin/albums_controller.rb:39:in `update'
  TRANSACTION (0.2ms)  BEGIN
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  Fund Load (0.5ms)  SELECT `funds`.* FROM `funds` WHERE `funds`.`id` IN (1, 6)
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  Section Load (0.3ms)  SELECT `sections`.* FROM `sections` WHERE `sections`.`id` = 5 LIMIT 1
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  Fund Exists? (0.3ms)  SELECT 1 AS one FROM `funds` WHERE `funds`.`name` = 'SomeFundA' AND `funds`.`id` != 1 LIMIT 1
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  Fund Exists? (0.2ms)  SELECT 1 AS one FROM `funds` WHERE `funds`.`name` = 'SomeFundB' AND `funds`.`id` != 6 LIMIT 1
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  Album Exists? (0.2ms)  SELECT 1 AS one FROM `albums` WHERE `albums`.`title` = 'SomeAlbum' AND `albums`.`id` != 38 LIMIT 1
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  Image Create (41.2ms)  INSERT INTO `images` (`file_name`, `title`, `alt`, `author`, `copyright`, `created_at`, `updated_at`) VALUES (NULL, NULL, NULL, NULL, NULL, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  AlbumImage Create (0.2ms)  INSERT INTO `album_images` (`album_id`, `image_id`, `created_at`, `updated_at`) VALUES (38, 231, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  Image Create (0.3ms)  INSERT INTO `images` (`file_name`, `title`, `alt`, `author`, `copyright`, `created_at`, `updated_at`) VALUES ('some-image.jpg', NULL, NULL, 'Some Author', 'SomeCopyRight', '2022-05-20 09:18:46', '2022-05-20 09:18:46')
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  ImageFund Create (0.3ms)  INSERT INTO `image_funds` (`image_id`, `fund_id`, `created_at`, `updated_at`) VALUES (232, 1, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  ImageFund Create (0.2ms)  INSERT INTO `image_funds` (`image_id`, `fund_id`, `created_at`, `updated_at`) VALUES (232, 6, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  AlbumImage Create (0.3ms)  INSERT INTO `album_images` (`album_id`, `image_id`, `created_at`, `updated_at`) VALUES (38, 232, '2022-05-20 09:18:46', '2022-05-20 09:18:46')
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
  TRANSACTION (3.1ms)  COMMIT
  ↳ app/controllers/admin/albums_controller.rb:40:in `update'
Redirected to http://localhost:3000/admin/albums/38
Completed 302 Found in 2136ms (ActiveRecord: 52.4ms | Allocations: 46882)

这是我的表格:

<%= form_for [:admin, @album], html: {role: 'form' } do |f| %>
  <%= render 'shared/error_messages', object: f.object %>
  <%= f.fields_for :images, Image.new do |ff| %>
    <div class='form-data'>
      <%= ff.label :file_name, class: 'form-label' %>
      <%= ff.file_field :file_name, multiple: :true, name: "album[images_attributes][][file_name]", class: 'form-field' %>
    </div>
    <div class="form-data">
      <%= ff.label :author, class: "form-label" %>
      <%= ff.text_field :author, name: "album[images_attributes][][author]", class: "form-field" %>
    </div>
    <div class="form-data">
      <%= ff.label :copyright, class: "form-label" %>
      <%= ff.text_field :copyright, name: "album[images_attributes][][copyright]", class: "form-field" %>
    </div>
    <div class="form-data">
      <%= ff.label :fund, class: "form-label" %>
      <% Fund.all.each do |fund| %>
        <%= check_box_tag "album[images_attributes][][funds_ids][]", fund.id, ff.object.funds.include?(fund), class: "checkbox" %>
        <%= fund.name %>
      <% end %>
    </div>
  <% end %>
  <div class='actions create'>
    <%= f.submit 'Save' %>
  </div>
<% end %>

为什么 images_attributes 得到一个包含两个元素的数组,而不是一个元素?

我希望有人能把我引向正确的方向。非常感谢您!

在相册 Controller 中,我为图像设置了允许的参数: 私有(private)

def album_params
  params.require(:album).permit(:title, :section_id, images_attributes: [:alt, :author, :copyright, :file_name, :title, funds_ids: []])
end

最佳答案

我能够让它运行起来。我需要在 file_field 中设置 include_hidden: false

<%= image.file_field :file_name, include_hidden: false, multiple: :true, name: "album[images_attributes][][file_name]", class: 'form-field' %>

在更新 Rails 和 Carrierwave 之前,这通常在没有这个选项的情况下工作。

关于ruby-on-rails - rails : multi-file upload produces one empty entry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72317021/

相关文章:

ruby-on-rails-3 - 使用 Carrierwave、Fog 到 S3 的 Rails 上传 - HTTP 与 HTTPS

ruby-on-rails - Carrierwave/Fog - 参数错误,无法识别提供者

css - SCSS/CSS 不加载 webpack

ruby-on-rails - Rails 中的 OAuth - 谷歌、推特、 Facebook 、连接登录,如 stackoverflow 登录

php - 在 apache + php 和新的 nginx rails 上并排运行旧站点

ruby-on-rails - 不同文件类型的载波文件上传

ruby-on-rails - Carrierwave 和 has_many

ruby-on-rails - 如何在仅API的Rails应用程序中使用devise gem进行身份验证

ruby-on-rails - Ruby 程序员与图形设计师的协作 : Best practices?

arrays - Rails 5 CarrierWave,无法删除多重上传中的最后一个文件