【问题标题】:Rails 4 nested form, how to display image thumb on edit formRails 4嵌套表单,如何在编辑表单上显示图像拇指
【发布时间】:2015-01-19 16:45:40
【问题描述】:
我正在使用 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>
如何在编辑表单上为每个图像显示拇指?目前编辑时只出现浏览按钮。
【问题讨论】:
标签:
ruby-on-rails
paperclip
nested-forms
【解决方案1】:
您可以使用以下代码显示拇指图像。(使用 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>
如果您使用的是 Carrierwave,您还需要在上传文件中指定拇指版本,例如
version :thumb do
process :resize_to_fit => [50, 50]
end
如果你使用的是回形针,那么你必须在模型中指定它
class Album < ActiveRecord::Base
has_attached_file :image_file, :styles => { :thumb => "50x50>" }
end
&你对回形针的看法将是
<%= image_tag image_form.object.image_file.url(:thumb) if image_form.object.image_file? %>