# Acts\_as\_attachment , someone using it?

**URL:** <https://www.ruby-forum.com/t/acts-as-attachment-someone-using-it/62146>\
**Category:** Rails\
**Created:** [May 31, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-someone-using-it/62146 "2006-05-31T22:01:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [May 31, 2006, 10:01pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-someone-using-it/62146/1 "2006-05-31T22:01:11Z")

</div>

Hi, i just found the acts\_as\_attachment plugin for image upload, seems  
sogood but i cant find any docs about it, if someone here using it can  
give some references or working examples about the plugin that will be  
excellent.

So what you wanna rails today?

---

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [June 1, 2006, 12:31am UTC](https://www.ruby-forum.com/t/acts-as-attachment-someone-using-it/62146/2 "2006-06-01T00:31:19Z")

</div>

I once took a closer look at Mephisto CMS/Blog engine, from the same  
author  
(Rick O.), which I think was using that plugin

---

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [June 1, 2006, 1:28am UTC](https://www.ruby-forum.com/t/acts-as-attachment-someone-using-it/62146/3 "2006-06-01T01:28:20Z")

</div>

ok, ill take a look cause i get a error:

undefined method `stringify\_keys!’ for  
#\<File:C:/DOCUME~1/ADMINI~1/CONFIG~1/Temp/CGI1976.1\>

im using the folloging code but doesnt seems to work:

#I have the table attachments defined like this:

# [http://technoweenie.stikipad.com/plugins/show/DatabaseSchema](http://technoweenie.stikipad.com/plugins/show/DatabaseSchema)

#Model---------  
class Attachment \< ActiveRecord::Base  
acts\_as\_attachment :content\_type =\> :image  
end

#view---------  
\<%= form\_tag({:action=\>‘upload\_image’}, :multipart=\>true) %\>  
Image File:\<%=  
file\_field\_tag(“attachment”) %\>  
\<%= submit\_tag(“Upload Image”) %\>  
\<%= end\_form\_tag %\>

#controller----------  
def upload\_image  
@attachment = Attachment.new(params[:uploaded\_data])  
if @attachment.save  
flash[:notice] = ‘Attachment was successfully created.’  
redirect\_to :action =\> ‘list’  
else  
flash[:notice] = ‘Error.’  
redirect\_to :action =\> ‘list’  
end  
end

---

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [June 1, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/acts-as-attachment-someone-using-it/62146/4 "2006-06-01T16:45:59Z")

</div>

On 5/31/06, Nhila D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> #Model---------
> 
> end  
> Sorry, writing docs is time consuming, and I’ve been busy.

I know it seems like NIH since there’s the file\_column plugin, but I  
originally wrote acts\_as\_attachments so I could store attachments in  
the DB for Mephisto (user avatars, templates, etc). As I started  
using it, it became apparent that larger files would need to be stored  
in the file system. So with the help of some other folks, I wrote the  
file system portion.

Your problem is that you passed a TempFile to Attachment.new(), which  
expects a hash. The Attachment model has a handy uploaded\_data method  
that handles the file upload. You just structure your form like this:

\<% form\_for :attachment do |f| -%\>  
\<%= f.file\_field :uploaded\_data %\>  
\<% end →

Then in the controller:

@attachment = Attachment.new(params[:attachment])  
@attachment.save  
…

This will automatically call @attachment.uploaded\_data =  
params[:attachment][:uploaded\_data], which sets the content type,  
filename, and attachment data. If you’re doing this from the command  
line to pre-populate attachments, you can just do this:

@attachment.new(:attachment\_data =\> IO.read(‘path/to/file’), :filename  
=\> ‘foo.gif’, :content\_type =\> ‘image/gif’)

I don’t know, maybe you can even do

File.open(‘foo.gif’) do |f|  
@attachment.new(:uploaded\_data =\> f)  
end

I hope this helps a bit.

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![Nhila\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5daacb/32.png) [@Nhila\_D](https://www.ruby-forum.com/u/Nhila_D)\
**Post date:** [June 1, 2006, 8:01am UTC](https://www.ruby-forum.com/t/acts-as-attachment-someone-using-it/62146/5 "2006-06-01T08:01:15Z")

</div>

Nhila D. wrote:

> Hi, i just found the acts\_as\_attachment plugin for image upload, seems  
> sogood but i cant find any docs about it, if someone here using it can  
> give some references or working examples about the plugin that will be  
> excellent.

is there not an Rdoc directory with it? if there isnt, you can generate  
it by running ‘rdoc’.

uploading is easy, a big ‘niggle’ that doesnt seem to be documented  
anywhere is if the file is small enough it puts the binary data in a  
StringIO object (in RAM), otherwise its a TempFile. after wondering why  
the heck certain files werent appearing i found something like this  
buried in 30K of code in the file\_column plugin:

i=params[:imagefile]  
if i.respond\_to? “local\_path” and i.local\_path and  
File.exists?(i.local\_path)  
FileUtils.mv(i.local\_path,imagedir+i.original\_filename)  
elsif i.respond\_to?(:read)  
File.open(imagedir+i.original\_filename, “wb”) {|f| f.write(i.read)}

cheers
