在编辑视图中显示附件 Rails 6(活动存储)

发布于 2025-01-10 19:11:45 字数 583 浏览 1 评论 0原文

情况: 我成功地在我的表单部分“_a_partial.html.erb”中上传了一个名为:physical_copy的文件,该文件用于创建新对象并同时编辑现有对象

代码:

              <%= f.file_field :physical_copy,
                               required: true,
                               class: 'form-control',
                               value: @tax_relevant_document.physical_copy  %>

New &创建作品完美无缺。

问题: 该文件不会显示,因此在编辑时会被删除,这不是计划的行为。我可以通过先下载然后“重新上传”来解决这个问题,但这不应该是正常行为。

猜测: 这个“值”的东西对我来说看起来很不对劲,但删除该属性并没有帮助。

我想:

  1. 编辑视图以将字段中的现有文件名显示为基本值表示
  2. 形式表单应包含字段中的现有附件

Situation:
I sucessfully upload a file called :physical_copy in my form partial "_a_partial.html.erb", which is used for creating new objects and editing existing objects at the same time

Code:

              <%= f.file_field :physical_copy,
                               required: true,
                               class: 'form-control',
                               value: @tax_relevant_document.physical_copy  %>

New & create works perfectly fine.

Problem:
The file is not shown and hence deleted when editing, which is not the planned behavior. I can work around by first downloading and "re-uploading" but this shouldn't be the normal behavior.

Guess:
This "value" thing looks awry to me but removal of the attribute didn't help.

I would like:

  1. edit view to show the existing filename in the field as basically value representation
  2. the form should include the existing attachment in the field

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2025-01-17 19:11:45

我找到了一个解决方案并使其成为有条件的,在我的情况下效果很好:

<% if !object.attachment_attribute.attached? %>
  (File Field from above)
<% else # (attachment exists) %>
  <%=
    link_to(
      object.attachment_attribute.filename, 
      rails_blob_path(object.attachment_attribute, disposition: 'attachment')
    )
  %>
<% end %>

基本上是下载附件!

I found a solution and made it conditional, which works fine in my case:

<% if !object.attachment_attribute.attached? %>
  (File Field from above)
<% else # (attachment exists) %>
  <%=
    link_to(
      object.attachment_attribute.filename, 
      rails_blob_path(object.attachment_attribute, disposition: 'attachment')
    )
  %>
<% end %>

Basically the attachment for download!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文