我正在创建多个附件导入的工具,我无法弄清的唯一问题是如何将文件名存储在文档字段中,因为它将文件名存储为数组。
这就是我要尝试的事情,
upload.rake
require 'fileutils'
namespace :request_document do
desc "Expects to get a file or folder ..."
task :import_attachment, [:filename] => [:environment] do |task, args|
source_directory = "/Users/admin/Documents/demo"
Dir.foreach(source_directory) do |filename|
if /^(?<client_id>\d+)/ =~ filename
puts filename
client = Client.find_by(
legacy_client_id: client_id
)
if client.nil?
puts "Client Not Found"
else
puts "Client found"
request = client.requests.last
# document_uploader = DocumentUploader.new
# File.open("#{source_directory}/#{filename}") { |f| document_uploader.store!(f) }
# document_uploader.store!("#{source_directory}/#{filename}")
target_directory = "public/uploads/request/documents/#{request.id}"
puts request.id
FileUtils.mkdir_p(target_directory)
FileUtils.cp("#{source_directory}/#{filename}", "#{target_directory}")
request.documents.push(['filename'])
request.save
end
end
end
end
end
请求模式
create_table "requests", force: :cascade do |t|
t.string "documents", default: [], array: true
end
我正在使用载波宝石来上传多个图像。
我得到的错误是:
NoMethodError: undefined method `identifier' for ["filename"]:Array