[英]How do you use ActiveStorage has_many_attached i active admin when editing images on a model?
[英]How to display Active Storage attached file when editing form?
我正在构建一个协议管理应用程序。 每个协议都有供应商、激活、到期日期和文件。 文件是 pdf 中的附加协议。 我使用 Active Storage 附加文件。 当我编辑表格时,我看不到附件。 因此,如果我更改任何其他参数,比如说供应商并更新协议,则先前附加的文件将被删除。
如果我在编辑时不上传任何其他文件,我应该怎么做才能使附加的文件名在更新时出现并且不会被删除?
更新:
这是代码。 除了添加:文件到允许的参数之外,我没有对生成的协议控制器进行任何调整。
agreement.rb
class Agreement < ApplicationRecord
belongs_to :vendor
has_one_attached :file
end
agreements_controller.rb
class AgreementsController < ApplicationController
def agreement_params
params.require(:agreement).permit(:vendor_id, :activation_date, :expiration_date, :file)
end
end
_form.html.erb
<%= form_with(model: agreement, local: true) do |form| %>
…
<div class="field">
<%= form.label :file %>
<%= form.file_field :file %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
添加协议 model
attribute :remove_file, :boolean, default: false
after_save :purge_file, if: :remove_file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.