How to save file names when loading a file using refile gem in rails application

I am trying to save the file name of the file that I upload to the rails application using refile gem. I created an API in the rails application to allow files to be downloaded via curl, but when I have the file, i.e. myfile.csv, and I load it into a rails application, it saves a file with a character hash name, i.e. 2HCF3F3...It also does not save the file extension.

My code is as follows:

class AddCsvFileToCsvFile < ActiveRecord::Migration
  def change
    add_column :csv_files, :csv_file_id, :string
    add_column :csv_files, :csv_file_filename, :string
    add_column :csv_files, :csv_file_content_type, :string
  end
end

Model # csv_file.rb

class CsvFile < ActiveRecord::Base

# validate :csv_extension
validate :file_extension_validation, :csv_size_validation, :if => "csv_file"

    # attachment :content_type => "text/csv"
    # http://ryanbigg.com/2009/04/how-rails-works-2-mime-types-respond_to/
    # attachment :csv, extension: "csv", content_type: "text/csv", raise_errors: true
    attachment :csv_file, extension: "csv", content_type: "application/octet-stream", raise_errors: true

private

    def file_extension_validation

    end

    NUM_BYTES_LIMIT = 4000
    def csv_size_validation
        # binding.pry
        errors.add(:csv_file, "file size can't exceed 4KB") if csv_file.size > NUM_BYTES_LIMIT
        # binding.pry
    end

    def csv_file_id
    end
# def csv_extension
#     unless csv_content_type == "text/csv"
#       errors.add :csv, "format must be csv"
#     end
#   end
    # def csv_extension
    #   unless File.extname(csv_filename) == "csv"
    #       errors.add :csv, "format must be csv"
    #   end
    # end
end
+4
source share

Source: https://habr.com/ru/post/1612671/


All Articles