【问题标题】:CarrierWave and Nested Form Gem RedisplayCarrierWave 和嵌套表单 Gem 重新显示
【发布时间】:2012-05-12 12:11:36
【问题描述】:

我安装了 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 变量,但是我需要在其中添加以下行以向用户显示文档的图像:

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

但是,这样做存在许多问题。首先 attachment_form 是引用 form_builder 而我想要实际的附件。其次,附件对文件一无所知。

可能需要使用另一种类型的循环机制,但我是 Ruby 新手,因此不胜感激。

谢谢大家!

【问题讨论】:

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


    【解决方案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 对不起,如果我误解了你的问题。

    【讨论】:

    • 太棒了!不完全是我需要的,但我不知道表单的“对象”属性......我想我正朝着正确的方向前进。谢谢!
    • 但是对象来自哪里。我对你的回答有点迷茫。我试图用蜻蜓宝石来做到这一点。
    猜你喜欢
    • 2012-05-14
    • 1970-01-01
    • 2012-04-04
    • 2023-03-30
    • 2016-09-13
    • 1970-01-01
    • 2016-04-04
    • 2016-08-17
    • 1970-01-01
    相关资源
    最近更新 更多