我想让我的应用程序使用 Shrine 上传多个文件,但是一个文档建议两个file_field
s 而另一个建议只一个。在他们的讨论论坛上发布问题后,有人建议我隐藏名为files[]
. 无论我是否这样做,第一个file_field
总是无法渲染。为什么这个字段不显示?
<%= form_for @item, html: { enctype: "multipart/form-data" } do |f| %>
<%= f.fields_for :photos do |i| %>
<%= i.label :image %>
<%= i.hidden_field :image, value: i.object.cached_photos_data, class: "upload-data" %>
<%= i.file_field :image, class: "upload-file" %> /// why is this not rendering?
<% end %>
<%= file_field_tag "files[]", multiple: true %> // what purpose does this one serve?
<%= f.text_field :title %>
<%= f.submit "Submit" %>
<% end %>
楷模:
class Item < ApplicationRecord
has_many :photos, as: :imageable, dependent: :destroy
end
class Photo < ApplicationRecord
include ImagesUploader::Attachment(:image)
belongs_to :imageable, polymorphic: true
validates_presence_of :image
end
控制器:
class ItemsController < ApplicationController
def new
@item = current_user.items.new
end
def create
@item = current_user.items.create(item_params)
@item.save
end
private
def item_params
params.require(:item).permit(:title, photos_attributes: { image: [] })
end
end