https://github.com/carrierwaveuploader/carrierwave/blob/master/README.md#multiple-file-uploads
特别是这条线给我带来了麻烦(我想):
<% user.avatars.each do |avatar| %>
<%= hidden_field :user, :avatars, multiple: true, value: avatar.identifier %>
<% end %>
<%= form.file_field :avatars, multiple: true %>
我有一张桌子叫
properties
还有一个相关的表叫做
property_units
这个
有carrierwave文件列。
_property_unit_fields.html.erb
<% if f.object.files.count > 0 %>
<div class="uploaded-files">
<% f.object.files.each do |file| %>
<%= f.hidden_field :files, multiple: true, value: file.file.identifier %>
<p>
<span><%= file.file.filename %></span>
</p>
<% end %>
</div>
<% end %>
<%= f.file_field :files, multiple: true %>
</div>
f
上面的例子是从这个茧状的实现:
<div id="property_units">
<%= f.fields_for :property_units do |unit| %>
<%= render 'property_unit_fields', f: unit %>
<% end %>
<div class='links'>
<%= link_to_add_association 'Add Unit', f, :property_units, class: 'button button--tertiary' %>
</div>
</div>
我可以保存文件,挂载的上传程序可以工作。我可以保存多个,但如果我再次上传,以前的文件将被完全替换。
例如,我刚刚上传了一个名为门票.pdf". 此文件在数据库中。
<input multiple="multiple" value="tickets.pdf" type="hidden" name="property[property_units_attributes][0][files][]" id="property_property_units_attributes_0_files">
一旦我上传了一个新文件,”门票.pdf“会被删除。
我做错什么了?
已存在并在隐藏输入中引用的文件是
tickets.pdf
新上传的文件是
ActionDispatch
显然,它发送的是一个字符串,Carrierwave需要一个文件。但这正是文件规定要做的。所以我不知道请求中还应该放些什么
"files"=>["tickets.pdf", #<ActionDispatch::Http::UploadedFile:0x00007fd6dc275118 @tempfile=#<Tempfile:/tmp/RackMultipart20200415-3213-10sfy1b.pdf>, @original_filename="somefile.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"property[property_units_attributes][0][files][]\"; filename=\"somefile.pdf\"\r\nContent-Type: application/pdf\r\n">], "_destroy"=>"false", "id"=>"1"}},
{NULL,"\"somefile.pdf\""}
无效票