Acts_as_attachment , someone using it?


#1

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?


#2

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


#3

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

#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


#4

On 5/31/06, Nhila D. 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


#5

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