# Ignore Empty File Uploads with attachment\_fu (Multiple files)

**URL:** <https://www.ruby-forum.com/t/ignore-empty-file-uploads-with-attachment-fu-multiple-files/172559>\
**Category:** Rails\
**Created:** [July 23, 2009, 9:44am UTC](https://www.ruby-forum.com/t/ignore-empty-file-uploads-with-attachment-fu-multiple-files/172559 "2009-07-23T09:44:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [July 23, 2009, 9:44am UTC](https://www.ruby-forum.com/t/ignore-empty-file-uploads-with-attachment-fu-multiple-files/172559/1 "2009-07-23T09:44:17Z")

</div>

Hi there,

I have multiple file uploads working with attachment\_fu, however if  
one of the file fields is empty then it throws a validation error.  
Removing the validation results in an empty (nil) record being  
created.

I’d like to be able to just ignore the empty file field and save the  
ones present.

Any help or advice would be much appreciated.

Many thanks,  
Will

# --------------------------------------------------- photo.rb

class Photo \< ActiveRecord::Base  
belongs\_to :album  
has\_attachment :content\_type =\> :image,  
:storage =\> :file\_system,  
:max\_size =\> 5000.kilobytes,  
:resize\_to =\> ‘600x600\>’,  
:thumbnails =\> { :thumb =\> ‘150x150\>’, :stamp =\>  
‘75x75\>’ },  
:path\_prefix =\> ‘public/images’,  
:processor =\> ‘rmagick’

validates\_as\_attachment  
end

# --------------------------------------------------- album.rb

class Album \< ActiveRecord::Base  
has\_many :photos, :dependent =\> :destroy  
after\_update :save\_photos

def image\_attributes=(image\_attributes)  
image\_attributes.each do |attributes|  
if attributes[:id].blank?  
photos.build(attributes)  
else  
photos = photos.detect { |t| t.id == attributes[:id].to\_i}  
photos.attributes = attributes  
end  
end  
end

def save\_photos  
photos.each do | photo |  
photo.save(false)  
end  
end  
end

# ---------------------------------------------------

albums\_controller.rb  
class AlbumsController \< ApplicationController

# … Other REST actions

def edit  
@collection = Collection.find(params[:id])  
end

def update  
@album = Collection.find(params[:id])  
if @album.update\_attributes(params[:album])  
flash[:notice] = “New photos saved”  
redirect\_to album\_path(@album)  
else  
flash[:error] = “Something went wrong”  
render :action =\> ‘edit’  
end  
end  
end

# --------------------------------------------------- views/albums/

edit.html.erb  
\<%= error\_messages\_for ‘album’ %\>

\<% form\_for (@album, :html =\> { :multipart =\> true }) do |f| %\>  
\<% fields\_for “album[image\_attributes][]”, @album.photos do |  
photo\_form| %\>

\<%= photo\_form.file\_field :uploaded\_data, :index =\> nil %\>  
Name: \<%= photo\_form.text\_field :name, :index =\> nil %\>

  

\<%= photo\_form.file\_field :uploaded\_data, :index =\> nil %\>  
Name: \<%= photo\_form.text\_field :name, :index =\> nil %\>

  
\<% end %\>

\<%= submit\_tag ‘save’ %\>  
\<% end %\>
