Topic: *Rails Beginner* Txt file upload to filesystem + DB

Hello all. I'm having some difficulty with the way that rails handles database writes vs. writes to hard drive. I can get my code to work to upload to the database, and to upload to the hard drive, but not both. I've tried replacing after_save :store_doc with just a direct call to the store_doc method, but that doesn't seem to work.

Can someone explain to me why this is? Without recommending a module to handle this, can someone guide me through writing a model that can accept a text upload, process the text into a DB and also write the file to a directory?

class Document < ActiveRecord::Base
        attr_accessible :body, :extension, :filename, :upload

        def upload=(incoming_file)
                        unless incoming_file.blank?
                        @incoming_file = incoming_file
              self.filename = incoming_file.original_filename
        self.extension = incoming_file.original_filename.split('.').last.downcase
                                if self.extension == 'txt'
                self.body =
                                        self.body = 'wrong file type for direct uploading'

#    def filename=(new_filename)
#        write_attribute("filename", sanitize_filename(new_filename))
#    end

    # File.join is a cross-platform way of joining directories;
    # we could have written "#{Rails.root}/public/doc_store"
    DOC_STORE = File.join Rails.root, 'public', 'doc_store'

     # where to write the first file to
    def document_filename
      File.join DOC_STORE, "#{id}.#{extension}"

    # where to write the txt file to
    def document_txtfilenamehas_document
      File.join DOC_STORE, "#{id}.txt"

    # return a path we can use in HTML for the docfile
    def document_html_path

    # if a doc file exists, then we has a docfile
    def has_document?
      File.exists? document_filename

after_save :store_doc


                def store_doc
       "NOW inside store_doc"
                        if @incoming_file
                                #make the DOCSTORE dir if it doesn't already exist
                                FileUtils.mkdir_p DOC_STORE
                                #write out the data to the file
                       "write out the data to the file"
                      , 'wb') do |f|

#                               @incoming_file = nil


Last edited by davidsmind (2013-02-07 06:10:45)

Re: *Rails Beginner* Txt file upload to filesystem + DB

Keep in mind, with the above code, If I upload anything that ISNT a txt file, it gets saved to the hard drive no problem.

Re: *Rails Beginner* Txt file upload to filesystem + DB

Why not just use ready-to-go solutions like Paperclip or attachment_fu (older one, may be outdated), CarrierWave or dragonfly?

Last edited by Javix (2013-02-07 08:24:36)

Re: *Rails Beginner* Txt file upload to filesystem + DB

Because all those things seem needlessly complicated for handling txt files and I'd like to learn some thing more about rails. Do you have an answer?

Re: *Rails Beginner* Txt file upload to filesystem + DB

*bump* so no answer here?