【问题标题】:Paperclip multiple file attachment: Error after file with wrong content_type chosen回形针多个文件附件:选择了错误 content_type 的文件后出错
【发布时间】: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 %>

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    试试这个:)

    (4 - @post.post_images.select(&:new_record?).count).times {@post.post_images.build}
    

    【讨论】:

    • 嘿,还有一件事。如果由于其他字段而导致表单提交错误,则将重置回形针 file_fields,并且我必须再次选择所有图像。你有解决方法吗?
    • @rookieRailer,你必须再次选择它们。这是安全问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多