我正在使用 CarrierWave 和 Nested Form Gem。使用 Carrierwave github 页面(https://github.com/jnicklas/carrierwave)上标题为“使上传工作跨表单重新显示”的部分,我能够成功存储缓存,这样即使用户犯了错误,然后文件被保存。我第一次点击页面并单击“上传文件”时,它可以工作并显示文件的名称以及指示已上传文件类型的小缩略图图标。
但是,文件输入按钮在重新显示时为空白,并且图标消失了。但是我知道它在那里,因为如果我再次提交有效信息并单独留下文件输入按钮,它会创建一个新资源。这是我的代码片段。
<%= nested_form_for @resource, :html=>{:multipart => true } do |f| %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<%= f.fields_for :attachments do |attachment_form| %>
<p>
<%= attachment_form.label :file %>
<% if !attachment_form.object.file.path.blank? %>
<% file_info = get_cache_file_info attachment_form.object.file.path %>
<%= image_tag(File.join('/tmp/cache/', file_info[:folder])) #THIS DISPLAY CORRECTLY %>
<% end %>
<%= attachment_form.file_field :file %>
<%= attachment_form.hidden_field :file_cache %>
</p>
<%= attachment_form.link_to_remove "Remove this attachment" %>
<% end %>
<%= f.link_to_add "Add attachment", :attachments %>
<p><%= f.submit %></p>
<% end %>
我想知道是否必须编辑 HTML 文件输入以显示图标,或者是否有其他 Rails 方式来处理这个问题。image_tag 也正确显示,但对于我的应用程序让用户上传文本文件,所以图像不是很有帮助。
提前致谢!
更新 我还尝试像这样设置“输入”标签的值:
<%= attachment_form.file_field :file, :value => "#{File.join(file_info[:folder], '/',attachment_form.object.file.identifier)}" %>
那也没有用。“输入”标签的值设置正确,但仍显示为“未选择文件”