rookieRailer
rookieRailer

Reputation: 2341

Paperclip multiple file attachment: Error after file with wrong content_type chosen

I am trying to get Paperclip to attach multiple images, 4 in my case, and followed all possible tutorials found on the internet. I can attach multiple images, without a problem. However, problems start once I attempt to attach a file with wrong content_type than an image. This is the error I get:

Post images photo content type is not one of image/jpg, image/jpeg, image/png, image/gif, which is perfectly fine.

However, after the error is rendered, I have five buttons to attach images, instead of four. I get an extra file_field button , which is due to the error generated, and the four file_filed buttons that I already have in my page. I could not figure out a way to fix this problem. Here's the code associated with this:

Controller code

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

Part of my View Code, _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 %>

Upvotes: 0

Views: 336

Answers (1)

fl00r
fl00r

Reputation: 83680

try this :)

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

Upvotes: 1

Related Questions