【发布时间】:2011-10-26 21:28:12
【问题描述】:
我正在尝试让 Paperclip 附加多个图像,在我的情况下为 4 个,并遵循互联网上所有可能的教程。我可以附加多个图像,没有问题。但是,一旦我尝试附加内容类型错误的文件而不是图像,就会出现问题。这是我得到的错误:
发布图片的照片内容类型不是 image/jpg、image/jpeg、image/png、image/gif 之一,完全可以。
但是,在呈现错误后,我有五个按钮来附加图像,而不是四个。我得到一个额外的 file_field 按钮,这是由于生成的错误,以及我的页面中已有的四个 file_filed 按钮。我想不出解决这个问题的方法。这是与此相关的代码:
控制器代码
def new
@post = Post.new
4.times {@post.post_images.build}
end
def create
@post = Post.new(params[:post])
if @post.save
redirect_to @post, :notice => "Successfully created post."
else
4.times {@post.post_images.build}
render :action => 'new'
end
end
我的部分查看代码,_form.html.erb
<%= f.fields_for :post_images do |builder| %>
<% if builder.object.new_record? %>
<div class="picture-field">
<%= builder.file_field :photo %>
</div>
<% end %>
<% end %>
【问题讨论】: