Forum: Ruby on Rails acts_as_attachment , someone using it?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
62736e9bac3bf829a614d9c94209b9cd?d=identicon&s=25 Nhila Dasgota (Guest)
on 2006-05-31 22:01
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?
30269682335f1fb247d71969fa715b5e?d=identicon&s=25 Roberto Saccon (rsaccon)
on 2006-06-01 00:31
(Received via mailing list)
I once took a closer look at Mephisto CMS/Blog engine, from the same
author
(Rick Olson), which I think was using that plugin
62736e9bac3bf829a614d9c94209b9cd?d=identicon&s=25 Nhila Dasgota (Guest)
on 2006-06-01 01:28
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) %>
 <label for="user_image_file">Image File:</label><%=
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
6f7c877de704c7cc03c8a3b2dc52df92?d=identicon&s=25 Carmen --- (carmen)
on 2006-06-01 08:01
Nhila Dasgota 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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-06-01 16:45
(Received via mailing list)
On 5/31/06, Nhila Dasgota <dasgot_k@gmail.com> 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 Olson
http://techno-weenie.net
This topic is locked and can not be replied to.