简体   繁体   English

file_field上载按钮在编辑表单上多次出现

[英]file_field upload button appearing multiple times on edit form

I have this code in my _form.html.erb file: 我的_form.html.erb文件中包含以下代码:

<%= f.fields_for :request_attachments do |ra| %>
    <div class="row westmontTextMuseo3" id="uploader">
        <div class="col-xs-12">
          <label class="btn btn-info"> Upload Files
                <%= ra.file_field :request_attachment_file, :multiple => true, name: "request_attachments[file][]", :style => "display: none", type: "file" %>
                <%= ra.hidden_field :file_cache %> 
          </label>
        <%= link_to(ra.object.file.url.to_s.split('/')[-1], ra.object.file.url) %>
        </div>
    </div>
<% end %>

I can see why the Upload Files button appears. 我可以看到为什么出现“ Upload Files按钮。 It seems like I want it outside the scope of the nested form, but I don't know how to do that. 似乎我希望它在嵌套表单的范围之外,但是我不知道该怎么做。

My goal is to place the button once after it has provided links to each of the already attached files. 我的目标是在按钮提供到每个已附加文件的链接之后放置一次。

Any help would be great, thanks. 任何帮助将是巨大的,谢谢。

Edit: Other code to see if we can get it to work. 编辑:其他代码,看是否可以使它正常工作。

<%= f.fields_for :request_attachments do |ra| %>
  <%= link_to(ra.object.file.url.to_s.split('/')[-1], ra.object.file.url) %>
<% end %>

<div class="row westmontTextMuseo3" id="uploader">
  <div class="col-xs-12">
    <label class="btn btn-info"> Upload Files
      <%= @request.request_attachments.file_field :request_attachment_file, :multiple => true, name: "request_attachments[file][]", :style => "display: none", type: "file" %>
      <%= @request.request_attachments.hidden_field :file_cache %> 
    </label>
  </div>
</div>

But this fails with an undefined method file_field' for #<RequestAttachment... 但这undefined method file_field' for #<RequestAttachment...undefined method file_field' for #<RequestAttachment...失败undefined method file_field' for #<RequestAttachment...

This seemed to have worked 这似乎有效

<%= f.fields_for :request_attachments do |ra| %>
  <div class="row">
    <div class="col-xs-12">
      <%= link_to(ra.object.file.url.to_s.split('/')[-1], ra.object.file.url) %>
    </div>
  </div>
<% end %>

<div class="row westmontTextMuseo3" id="uploader">
  <div class="col-xs-12">
    <label class="btn btn-info"> Upload Files
      <%= file_field_tag :request_attachment_file, :multiple => true, name: "request_attachments[file][]", :style => "display: none", type: "file" %>
      <%= hidden_field_tag :file_cache %> 
    </label>
  </div>
</div>

I assumed since the attributes were nested it wouldn't work, but it did work. 我假设由于属性是嵌套的,因此无法使用,但确实可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM