# File Attachment

**URL:** <https://www.ruby-forum.com/t/file-attachment/65827>\
**Category:** Rails\
**Created:** [August 21, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/file-attachment/65827 "2006-08-21T18:46:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 21, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/file-attachment/65827/1 "2006-08-21T18:46:44Z")

</div>

I have recently been looking for examples where it was possible to  
attach files along with a form submission, but I have found little about  
it. In the near future I plan to implement this functionality in my RoR  
web application and was wondering if there was any info or examples that  
can be provided.

-Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 21, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/file-attachment/65827/2 "2006-08-21T19:04:01Z")

</div>

Agile Web D. with Rails - 2nd Ed has a section on attaching  
images. That may help you.

Regards,  
Dave Dumaresq

* * *

Information and Educational Technology  
Kwantlen University College - 604-599-2120  
“So powerful is the light of unity that it can illuminate the whole  
earth.” --BahÃ¡'u’llÃ¡h

Gilles [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent by: removed\_email\_address@domain.invalid  
08/21/2006 09:46 AM  
Please respond to  
removed\_email\_address@domain.invalid

To  
removed\_email\_address@domain.invalid  
cc

Subject  
[Rails] File Attachment

I have recently been looking for examples where it was possible to  
attach files along with a form submission, but I have found little about  
it. In the near future I plan to implement this functionality in my RoR  
web application and was wondering if there was any info or examples that  
can be provided.

-Gilles

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 21, 2006, 8:08pm UTC](https://www.ruby-forum.com/t/file-attachment/65827/3 "2006-08-21T20:08:09Z")

</div>

scenarios which seem to be used a lot :

file\_column plugin

acts\_as\_attachment plugin

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 21, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/file-attachment/65827/4 "2006-08-21T20:19:58Z")

</div>

Gilles wrote:

> I have recently been looking for examples where it was possible to  
> attach files along with a form submission, but I have found little about  
> it. In the near future I plan to implement this functionality in my RoR  
> web application and was wondering if there was any info or examples that  
> can be provided.
> 
> -Gilles

Actually, this link: [Peak Obsession](http://manuals.rubyonrails.com/read/chapter/57)  
might be quicker for you and it’s free.

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 22, 2006, 12:48am UTC](https://www.ruby-forum.com/t/file-attachment/65827/5 "2006-08-22T00:48:32Z")

</div>

Hello Gilles,

For a second reference with working examples (ones I use myself) check  
out these posts–

First the basic form setup and Ruby needed to move files:  
[http://www.forthecode.com/user/index/15](http://www.forthecode.com/user/index/15)

Second a File Uploading Model to help move files on your filesystem and  
save data about those files to your database:  
[http://www.forthecode.com/user/index/21](http://www.forthecode.com/user/index/21)

This code definitely needs a bit of refactoring, but it regardless - it  
does work.

Regards,

Charles

Gilles wrote:

> I have recently been looking for examples where it was possible to  
> attach files along with a form submission, but I have found little about  
> it. In the near future I plan to implement this functionality in my RoR  
> web application and was wondering if there was any info or examples that  
> can be provided.
> 
> -Gilles

---

<div class="post-metadata">

**Author:** ![gilles](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ce7236/32.png) [@gilles](https://www.ruby-forum.com/u/gilles)\
**Post date:** [August 24, 2006, 7:28pm UTC](https://www.ruby-forum.com/t/file-attachment/65827/6 "2006-08-24T19:28:42Z")

</div>

Ahh, thanks for the links. So I have a text field that gets filled in  
with the file path after choosing a file by browsing the local hard  
drive. I realize that when submitted, the file goes to my windows/temp  
directory as a windows user. I haven’t done anything similar before.  
It’s saving it in the form CGIxxxx.x, where the x’s are numbers, much  
like in the forthecode walkthrough which I am following.

There are problems with moving the file out of the temp directory to an  
appropriate directory in my web application. When using the FileUtils.mv  
function I get a permission denied rails error in my browser. That file  
is being used by something, so I copied the file instead. I don’t know  
if there will be a problem with uploaded temp files overcrowding the  
temp folder. On a similar note, is there an optimal place to put  
uploaded files within my RoR directory structure?

Also, what about security issues. How do I prevent malicious files from  
entering my uploads directory with the potential to cause havoc or files  
that are extremely big taking up space, etc.?

Eventually, when all this is sorted out, how would I go about providing  
a link to the upload associated with the form I am viewing, so I can  
download it or view it in my browser?

I apologize for all the questions. I am unsure about many aspects of  
uploading files and was unable to find much information regarding it in  
ruby on rails.

-Gilles
