Topic: attachment_fu questions

I am having a hard time saving the uploaded files to the directory of my choice.

There is the method full_filename() that is said to be the one to override to specify a custom path, but when I do so the file is not saved

#Here is the original method in the file_system_backend.rb file

# Overwrite this method in your model to customize the filename.
        # The optional thumbnail argument will output the thumbnail's filename.
        def full_filename(thumbnail = nil)
          file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
          File.join(RAILS_ROOT, file_system_path, *partitioned_path(thumbnail_name_for(thumbnail)))
        end


I created a custom full_filename method in my Picture class, which was called on to generate the file name for the uploaded file, but when this method was used the file was not saved in the directory that I specified.

Here is the updated method within the Picture class

def full_filename(thumbnail = nil)
    file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
    File.join(RAILS_ROOT, "pictures", thumbnail_name_for(thumbnail))
  end

I admit I am a little confused on how this method overrides the the one in the file_system_backend.rb file (I will have to read up on this), but for starters I just want to figure out why the files are not saving.

There is also an issue in the thumbnails not being created.  Sometimes they are created as they should be, then after testing things at a later time, they are no longer saved.

Thanks for the help.

Last edited by iso (2007-08-24 16:43:39)

Re: attachment_fu questions

Here is my model code.  This took me a while to figure out so hopefully it will help you. 'ApplicantComment' belongs_to my main model, 'Applicant', which allows attchment of multiple file uploads.

:path_prefix changes the save location to  'myapp/public/file_attachments'.

partitioned_path(*args) generates sub-directories with the id of the parent applicant instead of the id of the comment.

class ApplicantComment < ActiveRecord::Base
  belongs_to :applicant

  has_attachment  :storage => :file_system, :path_prefix => "public/file_attachments/"

  def partitioned_path(*args)
    ("%04d" % applicant_id).scan(/..../) + args
  end
end

Last edited by ebrad (2007-10-18 16:17:05)