CarrierWave和嵌套表单宝石重新显示

时间:2012-05-03 00:29:19

标签: ruby-on-rails nested-forms carrierwave

我安装了CarrierWave(0.6.1)和Nested Form gem。我有一个包含许多附件的资源模型,它有一个FileUploader。

我有一个嵌套表单,用户可以使用一个资源上传多个文件。我正在关注github上的部分(https://github.com/jnicklas/carrierwave),该部分说明了如何在重新显示的情况下使上传工作不幸,这只是1:1的比例。

这是我的代码:

<%= nested_form_for @resource, :html=>{:multipart => true } do |f| %>
    <p>
        <%= f.label :title %>
        <%= f.text_field :title %>
    </p>
    <%= f.fields_for :attachments, @attachment do |attachment_form|  %>
      <p>
          <%= attachment_form.label :file %>
          <%= attachment_form.file_field :file %>
          <%= attachment_form.hidden_field :file_cache %>
          <%= image_tag(attachment_form.file_url) if attachment_form.file? # DOESN'T WORK!!! %>
      </p>
      <%= attachment_form.link_to_remove "Remove this attachment" %>
    <% end %>
    <%= f.link_to_add "Add attachment", :attachments %>
    <p><%= f.submit %></p>
<% end %>

一切正常,它填充了file_cache变量就好了attachment_form但是我不知何故需要在那里添加以下行来向用户显示文档的图像:

<%= image_tag(attachment_form.file_url) if attachment_form.file? %>

然而,这有很多问题。首先,attachment_form引用了form_builder,而我想要实际的附件。其次,附件对文件一无所知。

可能需要使用其他类型的循环机制,但我是Ruby的新手,所以任何帮助都会受到赞赏。

全部谢谢!

1 个答案:

答案 0 :(得分:1)

如果您尝试这样做:

<%= image_tag(attachment_form.object.file_url) if attachment_form.object.file? %>

您将能够显示以前上传的图像。但如果您想立即显示上传,则需要使用其他内容。例如:https://github.com/blueimp/jQuery-File-Upload/wiki/jQuery-File-Upload-v4-for-Rails-3 对不起,如果我误解了你的问题。