# How to upload multiple images with file\_column + rmagick

**URL:** <https://www.ruby-forum.com/t/how-to-upload-multiple-images-with-file-column-rmagick/133240>\
**Category:** Rails\
**Created:** [March 29, 2008, 6:59am UTC](https://www.ruby-forum.com/t/how-to-upload-multiple-images-with-file-column-rmagick/133240 "2008-03-29T06:59:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![beexk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@beexk](https://www.ruby-forum.com/u/beexk)\
**Post date:** [March 29, 2008, 6:59am UTC](https://www.ruby-forum.com/t/how-to-upload-multiple-images-with-file-column-rmagick/133240/1 "2008-03-29T06:59:08Z")

</div>

how to upload multiple images with file\_column + rmagick in one form?

---

<div class="post-metadata">

**Author:** ![beexk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e5b9ba/32.png) [@beexk](https://www.ruby-forum.com/u/beexk)\
**Post date:** [March 29, 2008, 7:59am UTC](https://www.ruby-forum.com/t/how-to-upload-multiple-images-with-file-column-rmagick/133240/2 "2008-03-29T07:59:35Z")

</div>

I have done this before using file\_column, but have since switched to  
attachment\_fu. The process should be about the same though. First  
off, these links will probably help alot.

[http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/](http://the-stickman.com/web-development/javascript/upload-multiple-files-with-a-single-file-element/)  
[http://www.silverscripting.com/blog/2007/12/18/multiple-file-upload-with-rails-file\_column-plugin-and-mootools/](http://www.silverscripting.com/blog/2007/12/18/multiple-file-upload-with-rails-file_column-plugin-and-mootools/)

They both deal with mootools, to make multiple image uploads a bit  
snazzier rather than limiting to so many files per form (or littering  
your page with a mess of file fields). If you don’t want to use the  
snazzy javascripts, then just create your fields and name them  
something like ‘attachments[]’ so that the files get pushed into an  
array that rails can use like ‘params[:attachments]’

As for the controller, mine looks like this (again I’m using  
attachment\_fu, but it should be identical)

def create  
@picture = @item.pictures.build(params[:picture])  
@pictures = @item.pictures.find(:all)

```
unsaved_pictures = []
saved_pictures = []
params[:attachments] ||= []
params[:attachments].each do |file|
  @picture = @item.pictures.build({:uploaded_data => file}) unless

```

file == “”  
unsaved\_pictures \<\< @picture.filename unless @picture.save  
saved\_pictures \<\< @picture.filename if @picture.save  
end

```
if saved_pictures.blank?
  flash[:error] = "No Pictures were added."
else
  if saved_pictures.size < 2
    flash[:notice] = "Picture was successfully added.

```

(#{saved\_pictures[0]})"  
else  
flash[:notice] = “Pictures were successfully added.  
(#{saved\_pictures.join(', ')})”  
end  
end

```
unless unsaved_pictures.blank?
  if unsaved_pictures.size < 2
    flash[:error] = "File was not added. (#{unsaved_pictures[0]})"
  else
    flash[:error] = "Files were not added.

```

(#{unsaved\_pictures.join(', ')})"  
end  
end

```
respond_to do |format|
  format.html { redirect_to(item_pictures_url) }
end

```

end

The first to lines should be familiar, then I create empty arrays,  
saved\_pictures and unsaved\_pictures so that I can report stuff back to  
the user later. Then I iterate through the attachments making sure  
the file field wasn’t empty ( I probably should have used file.empty?  
instead of file == “” there). If the picture wasn’t saved I store the  
name into my unsaved\_pictures array, if it was I store the filename in  
saved\_puctures array. Then I just use flash to send out notices and  
errors depending on what was/wasn’t saved and proper pluralization.

Hope this helps get you going in the right direction.

On a side note if anyone has any suggestions on where I can make this  
code better, please do tell me 😃 The method is a bit beefy and there  
are singular/plural flash notices doesn’t seem to dry.

Also has anyone used Paperclip for file attachments yet? If so, how is  
it?

On Mar 28, 11:59 pm, Shih T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
