I’m a Richardson, RU?

06 May, 2007

Tweaking Upload Location with Attachment_Fu

Posted by: imarichardson In: Programming & Development

So it seems like everyone’s agreeing that Attachment_Fu (tutorial here) is the Acts_As_Authenticated new model Liquid Metal Terminator version 2 replacement for handling file uploads. The only problem is that for an application I’m developing I need to specify a specific location for each upload based on a particular primary key id rather than just saving into a directory based on the file id.

So after gleaning from “almost effortless’” Working with attachment_fu article this is what I did:

Yea, that’s it. For the “change_filename” method I set a default “use_path” variable because the model needed an initial value and wouldn’t process unless it was there. Then I overwrite the filename location in the database to the “use_path” value with the “MyModel_id” (this will be the folder) and the “filename” (value from the form) appended.

Finally, the “full_filename” method overwrites the value that the attachment_fu plugin uses for the default directory to the “use_path” directory I specified. And in true RubyOnRails form, all this functionality goes in the Model and not in the controller.

Hope this helps someone in the great “out there”.

(DISCLAIMER: Yea, there may be a better way of doing this, in fact I know I can re-factor the use_path for both functions … and I admit I’m no expert, so feel free to let me know. Just, please be kind to the Rich-man.)

Technorati Tags:

1 Response to "Tweaking Upload Location with Attachment_Fu"

Comment Form

You must be logged in to post a comment.

I'mARichardson Who?

Who am I? Well, let's see: I'm a husband, father, brother, God-father, down to earth, relaxed, happy kinda guy who likes clean code, long walks on the beach, takes his laptop with him everywhere he goes, and (as I'd like to think) all around good guy!