ruby 使用rails activestorage上传多个文件的正确方法是什么

ifmq2ha2  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(96)

我正在尝试上传多个“支持照片”文件到我的项目模型。
支持的照片文件具有has_many_attached关联,因为要上载多个文件。

item.rb

class Item < ApplicationRecord
    belongs_to :user
   

    has_one_attached :cover_photo do |attachable|
        attachable.variant :medium, resize_to_limit:[300,300]
        attachable.variant :large, resize_to_limit:[1200,1000]
    end

    has_many_attached :supporting_photos do |attachable|
        attachable.variant :medium, resize_to_limit:[300,300]
        attachable.variant :large, resize_to_limit:[1200,1000]
    end

在用户要填写的表单中,我有多个带有file_field标签的div,这样用户就可以附加多个“支持照片”。

items/new.html.erb

<div class="mt-2 flex justify-center rounded-lg border border-dashed border-gray-900/25 px-6 py-10">
                    <div class="text-center">
                      
                      <div class="mt-4 flex text-sm leading-6 text-gray-600">
                      
                       <%= form.file_field :supporting_photos, direct_upload:true %>
                   
                      </div>
                       <p class="text-xs leading-5 text-gray-600">or drag and drop</p>
                      <p class="text-xs leading-5 text-gray-600">PNG, JPG, GIF up to 10MB</p>
                    </div>
                  </div>
                </div>
                <div  class="sm:col-span-2">
                
                  <div class="mt-2 flex justify-center rounded-lg border border-dashed border-gray-900/25 px-6 py-10">
                    <div class="text-center">
                      
                      <div class="mt-4 flex text-sm leading-6 text-gray-600">
                
                      <%= form.file_field :supporting_photos, direct_upload:true %>
                   
                      </div>
                       <p class="text-xs leading-5 text-gray-600">or drag and drop</p>
                      <p class="text-xs leading-5 text-gray-600">PNG, JPG, GIF up to 10MB</p>
                    </div>
                  </div>
                </div>

                <div  class="sm:col-span-2">
                 
                  <div class="mt-2 flex justify-center rounded-lg border border-dashed border-gray-900/25 px-6 py-10">
                    <div class="text-center">
                      
                      <div class="mt-4 flex text-sm leading-6 text-gray-600">
                
                        <%= form.file_field :supporting_photos, direct_upload:true %>
                   
                      </div>
                       <p class="text-xs leading-5 text-gray-600">or drag and drop</p>
                      <p class="text-xs leading-5 text-gray-600">PNG, JPG, GIF up to 10MB</p>
                    </div>
                  </div>
                </div>

最后,这里是我的项目控制器,当我查找如何做到这一点,这是我找到的解决方案,但它给了我一个错误Unpermitted parameter: :supporting_photos. Context: { controller: ItemsController, action: create, request: #<ActionDispatch...'在我的终端控制台,也undefined method each' for #<actiondispatch在浏览器中,当我加载的形式
”有人知道正确的方法吗?谢谢你,谢谢你**

def new
    # @item = item.new
    @item = current_user.items.build
  end

  def create
    # @item = item.new(item_params)
    @item = current_user.items.build(item_params.except(:supporting_photos))
    supporting_photos = params[:item][:supporting_photos]

    if supporting_photos
      supporting_photos.each do |supporting_photo|
        @item.supporting_photos.attach(supporting_photo)
      end
    end
    
    if @item.save
      
      redirect_to sell_sale_path
    else
      render :new, status: :unprocessable_entity
    end
  end

  private
    def item_params
      params.require(:item).permit(:name, :brand, :colour, :condition, :department, :category, :price, :is_sold, :cover_photo, :description, supporting_photos: [])
    end

我也想多个文件字段,因为我想使它更直观的用户这就是我去

与thisx 1c 1d 1x相反

u4vypkhs

u4vypkhs1#

不需要多个文件字段。只有一个multiple: true属性
另外请注意,您需要使用:item键 Package 此参数以允许它。基本上可以使用form_with:model键来实现此目的:

<%#= app/views/items/new.html.erb %>

<%= render "form", item: @item %>
<%#= app/views/items/_form.html.erb %>

<%= form_with(model: item) do |form| %>
  <%= form.file_field :supporting_photos, direct_upload: true, multiple: true %>

  <%= form.submit %>
<% end %>

但如果您需要 * 在更新 * 项目时保留现有照片并重复使用此表单部分,则可能需要添加多个隐藏字段:

<% item.supporting_photos.each do |sp| %>
  <%= form.hidden_field :supporting_photos, multiple: true, value: sp.signed_id %>
<% end %>

在后端方面,您不需要显式地附加照片(特别是在保存之前)或通过参数添加,只需:

# app/controllers/items_controller.rb

def create
  @item = Item.new(item_params)

  if @item.save
    redirect_to item_url(@item)
  else
    render :new, status: :unprocessable_entity
  end
end

private

def item_params
  params.require(:item).permit(supporting_photos: [])
end

相关问题