我有两个型号, Evidence 和 Attachment . 证据有很多附件,我使用CarrierWave来处理附件上传 .
我可以轻松地创建具有多个附件的证据,但是一旦创建了证据,我似乎无法添加其他附件 . 表单工作正常,但附件不保存 .
这是我的代码的基本设置:
(APP /模型/ evidence.rb)
class Evidence < ActiveRecord::Base
has_many :attachments, as: :attachable
accepts_nested_attributes_for :attachments, allow_destroy: true
end
(APP /模型/ attachment.rb)
require 'digest'
class Attachment < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
belongs_to :attachable, polymorphic: true
attr_accessor :override_filename
delegate :file, to: :attachment
after_create do
AttachmentFilesizeJob.perform_later(self)
AttachmentMd5sumJob.perform_later(self)
end
end
(应用程序/视图/证据/ _form.html.erb)
<%= simple_form_for(@evidence) do |f| %>
<%= f.error_notification %>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Attachments</h3>
</div>
<div class="panel-body">
<table class="table table-condensed table-striped">
<thead>
<tr>
<th>File</th>
<th colspan="1"></th>
</tr>
</thead>
<tbody id="attachments_table">
<%= f.fields_for :attachments do |af| %>
<% if af.object.attachment.nil? %>
<%= render 'attachment_fields', f: af %>
<% else %>
<tr>
<td><code><%= af.object.attachment %></code><%= attachment_download_button(af.object) %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<div id="links">
<%=
link_to_add_association 'Add Attachment', f, :attachments,
:"data-association-insertion-node" => 'tbody#attachments_table',
:"data-association-insertion-method" => 'append',
class: 'btn btn-primary btn-sm'
%>
</div>
</div>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
(应用程序/视图/证据/ _attachment_fields.html.erb)
<tr class="nested-fields form-inline form-table-row">
<td>
<%= f.file_field :attachment %>
</td>
</tr>
我该怎么做才能为已经创建的证据添加附件?创建新证据时一切正常,但编辑时却没有 .
证据上的参数#create(带有两个附件 - strain1.csv和strain2.csv)
参数:{“utf8”=>“✓”,“authenticity_token”=>“rKq1zaDAc1w1GlgF8Q ejjVuA6YXYc5EfhCwHXltK / 4vijOJw1ffeHUY3GWSSmKLevbfxBSwMAeEIjuHw4BIhQ ==”,“evidence”=> {“source”=>“”,“notes”=>“”,“ mutation_id“=>”“,”attachments_attributes“=> {”1476226326082“=> {”attachment“=>#,@ original_filename =”strain1.csv“,@ content_type =”text / csv“,@ headers =”Content-“处理:form-data; name = \“evidence [attachments_attributes] [1476226326082] [attachment] \”; filename = \“strain1.csv \”\ r \ nConContent-Type:text / csv \ r \ n“>}, “1476226412102”=> {“attachment”=>#,@ original_filename =“strain2.csv”,@ content_type =“text / csv”,@ headers =“Content-Disposition:form-data; name = \”evidence [attachments_attributes ] [1476226412102] [attachment] \“; filename = \”strain2.csv \“\ r \ nConContent-Type:text / csv \ r \ n”>}}},“commit”=>“创建证据”}
这两个附件保存得很好 . 然后我尝试编辑证据并添加一个额外的附件,“strain3.csv” - 这里是证据#date更新的参数:
参数:{“utf8”=>“✓”,“authenticity_token”=>“QsYgeVnsTf8oegVaJ733JD93MPxnH1aXutC9dDDAf53B5qY9Onvh22h4gTpE AshcO / snmTOqNRA4jbuii0c5g ==”,“evidence”=> {“source”=>“”,“notes”=>“”,“ mutation_id“=>”“,”attachments_attributes“=> {”0“=> {”id“=>”87“},”1“=> {”id“=>”88“},”1476226681765“=> {“attachment”=>#,@ original_filename =“strain3.csv”,@ content_type =“text / csv”,@ headers =“Content-Disposition:form-data; name = \”evidence [attachments_attributes] [1476226681765] [附件] \“; filename = \”strain3.csv \“\ r \ nConContent-Type:text / csv \ r \ n”>}}},“commit”=>“更新证据”,“id”=>“ 1" }
第三个附件永远不会上传或保存 .