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 :file_extension_validation, :csv_size_validation, :if => "csv_file"
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
errors.add(:csv_file, "file size can't exceed 4KB") if csv_file.size > NUM_BYTES_LIMIT
end
def csv_file_id
end
end
source
share