我有一个可能包含许多文件的对象的嵌套表单。我在创建对象时没有问题,但是当我需要编辑它时,我发现了问题,表单应该有文件 url 的链接,但我不知道如何引用文件本身。我用回形针。在这种情况下,如果只有一个文件并且它在对象模型中,我会做类似@object.file.url 但由于有很多文件,我不知道在这种情况下如何进行......
编辑.html.haml
= form_for @object, :url => { :action => "update", :controller => "object", :id => @object.id, :method => "post" }, :html => { :multipart => true } do |f|
- render "object/object_form", object: f.object, f: f
object_form.html.haml
.field
= f.label :name
= f.text_field :name
= f.fields_for :files do |builder|
= render 'object/file_fields', :f => builder
%p= link_to_add_fields "Add new file", f, :file
.field
= f.submit "Save"
file_fields.html.haml
%fieldset
.field
= f.label :file
= f.file_field :file
.field
= f.hidden_field :_destroy
= link_to "Remove File", '#', class: "remove_fields"
我正在尝试做的事情显然在 file_fields.html.haml 中不起作用
%fieldset
.field
= f.label :file
= f.file_field :file
= link_to f.file.url
.field
= f.hidden_field :_destroy
= link_to "Remove File", '#', class: "remove_fields"