【问题标题】:Delete a .csv file after import from a server [duplicate]从服务器导入后删除 .csv 文件 [重复]
【发布时间】:2014-10-15 16:39:59
【问题描述】:

我可以从我的服务器导入我的 .csv 文件,但现在我只想在导入完成后将其删除。我在 rake 任务中有所有代码,我正在尝试在任务完成后删除 events.csv 文件。这是我的代码。

require 'csv'
require 'open-uri'


namespace :import_incidents_csv do

task :create_incidents => :environment do
    puts "Import Incidents"

    #csv_text = File.read('/Users/Ben/Sites/ror/LFD/incidents.csv', :encoding => 'windows-1251:utf-8')
    csv_text = open("http://www.##########.com/###########/incidents.csv") {|f| f.read}
    csv = CSV.parse(csv_text, :headers => true)

    @incident_id_array = []
    @report_nr_array = []
    csv.each do |row|
        row = row.to_hash.with_indifferent_access
        Incident.create!(row.to_hash.symbolize_keys)
        @incident_id_array << Incident.last.id
        @report_nr_array << Incident.last.report_nr
    end

    #------This combines the incidents array and the report_nr array into a hash

    @report_incident_hash = {}
    @report_nr_array.each_with_index do |value, index|
        @report_incident_hash[value] = @incident_id_array[index]
    end
    #puts @report_incident_hash

    #----------------------------------------------------------------------------

    end
end

【问题讨论】:

    标签: ruby-on-rails ruby csv rake


    【解决方案1】:

    如果您可以通过 ssh 连接到服务器,请尝试使用 net/ssh gem 并运行“rm”命令

    【讨论】:

      猜你喜欢
      • 2015-09-25
      • 2021-11-03
      • 2015-11-27
      • 2016-09-20
      • 2014-06-30
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多