我有一个可能包含许多文件的对象的嵌套表单。我在创建对象时没有问题,但是当我需要编辑它时,我发现问题,表单应该有文件URL的链接,但我不知道如何引用文件本身。我正在使用回形针。在这种情况下如果只有一个文件并且它在对象模型中我会做类似@ object.file.url的事情,但由于有很多文件,我不知道如何继续这种情况......
edit.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"
答案 0 :(得分:1)
我认为你应该可以做到
f.object.url
所以:
%fieldset
.field
= f.label :file
= f.file_field :file
# Not sure if it would produce a Nil error on the new form.
# given how the f object wouldn't yet have a url
- if f.object.url
= link_to f.object.url
.field
= f.hidden_field :_destroy
= link_to "Remove File", '#', class: "remove_fields"