Rails 4嵌套表单,如何在编辑表单上显示图像拇指

bee*_*soh 1 ruby-on-rails paperclip nested-forms

我使用rails 4 nested_form为我的模型创建一个表单.模型相册有很多图像.上传图片的部分表格如下:

<div class="field">
    <%= f.fields_for :images do |image_form| %>
        <div>
          <%= image_form.file_field(:image_file) %>
          <%= image_form.link_to_remove "Remove this image" %>
        </div>
    <% end %>
</div>
Run Code Online (Sandbox Code Playgroud)

如何在编辑表单上为每个图像显示一个拇指?目前只有编辑时出现浏览按钮.

NSS*_*NSS 5

您可以使用以下代码显示拇指图像.(使用Carrierwave)

<div class="field">
  <%= f.fields_for :images do |image_form| %>
    <div>
      <%= image_form.file_field(:image_file) %>
      <%= image_tag image_form.object.image_file_url(:thumb) if image_form.object.image_file?  %>
      <%= image_form.link_to_remove "Remove this image" %>
    </div>
  <% end %>
</div>
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Carrierwave,您还需要在上传器文件中指定缩略图版本

version :thumb do
  process :resize_to_fit => [50, 50]
end
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Paperclip,则必须在类似的模型中指定它

class Album < ActiveRecord::Base
  has_attached_file :image_file, :styles => { :thumb => "50x50>" }
end
Run Code Online (Sandbox Code Playgroud)

&你对回形针的看法将会有

<%= image_tag image_form.object.image_file.url(:thumb) if image_form.object.image_file? %>
Run Code Online (Sandbox Code Playgroud)