带有Rails的神社上传了多个多态图像

Kev*_*own 2 image-uploading ruby-on-rails-5 shrine

我已经苦苦挣扎了大约5个小时,试图了解Shrine为什么阻止了我的上传。我要么在强参数中收到诸如“神殿:无效文件”之类的错误,要么出现“预期数组但有字符串”之类的错误。如果没有错误,则实际上不会保存图像。

require "image_processing/mini_magick"

class ImageUploader < Shrine
  include ImageProcessing::MiniMagick

  plugin :activerecord
  plugin :backgrounding
  plugin :cached_attachment_data
  plugin :determine_mime_type
  plugin :delete_raw
  plugin :direct_upload
  plugin :logging, logger: Rails.logger
  plugin :processing
  plugin :remove_attachment
  plugin :store_dimensions
  plugin :validation_helpers
  plugin :versions

  Attacher.validate do
    validate_max_size 2.megabytes, message: 'is too large (max is 2 MB)'
    validate_mime_type_inclusion ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
  end

  def process(io, context)
    case context[:phase]
    when :store
      thumb = resize_to_limit!(io.download, 200, 200)
      { original: io, thumb: thumb }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)
class Image < ActiveRecord::Base
  include ImageUploader[:image]
  belongs_to :imageable, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)
class Product < ApplicationRecord

  has_many :images, as: :imageable, dependent: :destroy    
  accepts_nested_attributes_for :images, allow_destroy: true
...
Run Code Online (Sandbox Code Playgroud)
# Strong Params: 

def product_params
  params.require(:product).permit(
    :name, :brand_id, :category_id, :price, :compare_price, :description,
    images_attributes: { image: [] },
    product_properties_attributes: [:id, :property_id, :value]
  )
...
Run Code Online (Sandbox Code Playgroud)

我的看法是:

  <%= f.fields_for :images do |image_form| %>
    <%= image_form.file_field :image, multiple: true %>
  <% end %>
Run Code Online (Sandbox Code Playgroud)

根据我在文档或gorails上阅读的所有内容,这应该可行。我需要重组images_attributes哈希吗?我也尝试使用direct_uploads,但是很难使presigned_url与S3一起使用。

重新归档使此操作非常容易,所以我可能会大声疾呼。

我显然在做错什么吗?

jan*_*o-m 5

根据fields_for文档,将为project.images集合中的每个图像调用提供的块。因此,如果您的产品当前没有任何图像,则该块将不会被调用(根据文档)。

为了使嵌套属性起作用,创建产品时需要转发以下参数:

product[images_attributes][0][image] = <file object or data hash>
product[images_attributes][1][image] = <file object or data hash>
product[images_attributes][2][image] = <file object or data hash>
...
Run Code Online (Sandbox Code Playgroud)

如果您查看“ 多个文件 ”神社指南,建议您只有一个文件字段即可接受多个文件:

<input type="file" name="file" multiple>
Run Code Online (Sandbox Code Playgroud)

然后使用Uppy为此字段设置直接上载,image为每个使用上载文件数据哈希填充的上载文件动态生成该字段:

<input type="hidden" name="product[images_attributes][0][image]" value='{"id":"...","storage":"cache","metadata":{...}}'>
<input type="hidden" name="product[images_attributes][1][image]" value='{"id":"...","storage":"cache","metadata":{...}}'>
....
Run Code Online (Sandbox Code Playgroud)

另外,您可以让用户附加多个文件,这些文件都提交给应用程序,然后在控制器中对其进行解构:

class ProductsController < ApplicationController
  def create
    images_attributes = params["files"].map { |file| {image: file} }
    Product.create(product_params.merge(images_attributes: images_attributes))
  end
end
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您必须确保HTML表单enctype="multipart/form-data"设置了属性(否则,将仅提交文件的文件名,而不提交文件本身)。