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