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: RubyOnRails
Feedback